Home > Enterprise >  How to do linearSearch to two ArrayList<String>?
How to do linearSearch to two ArrayList<String>?

Time:11-02

I received a homework assignment to create a shopping list program where it will ask the user for a list of ingredients, and after the user enters them, it will compare the inputs to a "pantry list" to see if everything is available. If yes, then it will print out "You got everything you need!" and if no, it will print out "You still need" "item that is missing." The specific instructions are:

  1. A pre-created list for pantry items
  2. User input into an ingredient list
  3. Pass the ingredient list to a method
  4. Use a conditional and loop in the method
  5. Print out the results of whether the user needs to go shopping based on the items in the ingredient list that are not in the pantry.

Below is my code:

import java.util.Scanner;
import java.util.ArrayList;

public class TheList
{
    public static String linearSearch(ArrayList<String> pantry, ArrayList<String> input)
    {
        for (int i = 0; i < pantry.size(); i  )
        {
            if (pantry == input)
            {
                return "You got everything you need!";
            }
        }
        return "You still need something!";
    }
    public static void main(String[] args)
    {

        // Create list for pantry items
        ArrayList<String> pantry = new ArrayList<String>();
        pantry.add("Bread");
        pantry.add("Peanut Butter");
        pantry.add("Chips");
        pantry.add("Jelly");
        //Create list for input items
        ArrayList<String> input = new ArrayList<String>();
        input.add("ingredientOne");
        input.add("ingredientTwo");
        input.add("ingredientThree");
        input.add("ingredientFour");
        // Execution
        input();
        System.out.println(linearSearch(pantry, input));
       
    }
    private static void input()
    {
        Scanner ingredientScan = new Scanner(System.in);
        
        System.out.println("Please enter an ingredient: ");
        String ingredientOne = ingredientScan.nextLine();
        System.out.println(ingredientOne   " Done.");
        
        System.out.println("Please enter an ingredient: ");
        String ingredientTwo = ingredientScan.nextLine();
        System.out.println(ingredientTwo   " Done.");
        
        System.out.println("Please enter an ingredient: ");
        String ingredientThree = ingredientScan.nextLine();
        System.out.println(ingredientThree   " Done.");
        
        System.out.println("Please enter an ingredient: ");
        String ingredientFour = ingredientScan.nextLine();
        System.out.println(ingredientFour   " Done.");
    }
    
}

What am I missing? This is pretty amateur, but I am a beginner and really need some help!

My main question is the if part for the loop in the linearSearch string. When I execute the program, it always print out "You still need something!" As for which thing is missing, I have no clue where to start in that aspect.

CodePudding user response:

I am assuming by linear search you mean searching if the pantry has everything in one pass. For scenarios such as these using a Set is the best option because in a set you can search for a key in constant time.

private void search(Set<String> pantry,Set<String> ingredients){
    for(String ingredient : ingredients){
        if(!pantry.contains(ingredient)){
            System.out.println("Something is missing");
            return;
        }
    }
     System.out.println("Everything is available");
     return;
}
 //Create a set like this
public void main(){
    Set<String> pantry = new HashSet<>();
}

CodePudding user response:

The answer is not hard, and since it's an assignment, maybe you need some advice:

  • The loop in the code is not utilized.
  • Also objects can't be compared using ==, it can only be compared using equals and in general you need to rewrite it.
  • Your problem is not that you need to use equals to compare, but that you need to do difference sets
  • Related