lundi 13 avril 2020

How to compare two arrays of different lengths in java? [duplicate]

I want to compare two String arrays of different lengths and test if there is a match in the content. I wrote the following java code, which works:

public class AuswahlTest2{

    public static void main(String[] args){
        String[] ingredients = new String[]{"garlic", "Parmesan"};
        String[] meal = new String[]{ "Spaghetti Aglio e Olio e Peperoncini", "Spaghetti", "garlic", "Olive oil", "Chili", "Parmesan", "persil"};

        generating(ingredients, meal);
    }

    public static void generating(String[] ingredients, String[] meal){

        for(int i=0; i<ingredients.length; i++){    

            for(int j=0; j<meal.length; j++){
                if(ingredients[i] == meal[j]){
                    System.out.println(" Match found. How about "+meal[0]);
                    break;
                }   
            }
        }
    }
}

However, I want to create the ingredients array myself by asking the user what ingredients he wants to use and thereby fill the array. I wrote the following code, however now the generating method does not find a match anymore, even if I type the same ingredients (parmesan and garlic). I have no idea why, and would be very glad for any help!

public class EatPasta2{

    public static void main(String[] args){     

        String[] meal = new String[]{ "Spaghetti Aglio e Olio e Peperoncini", "Spaghetti", "garlic", "Olive oil", "Chili", "Parmesan",                          "Persil"};  

        boolean ingredient = chosemeal();

        if (ingredient == true){

            int amount = Terminal.askInt("\nHow many ingredients? \n");
            String[] ingredients = new String[amount];



            for(int i=0; i<ingredients.length; i++){

                int x = i+1;

                ingredients[i] = Terminal.askString("Enter "+x+" ingredient: ");    

            }
        generating(ingredients, meal);

        }
        else{
        System.out.println("Alright, I will propose something");}
    }


    public static boolean chosemeal()
    {
        boolean ingredient = false;

        int course = Terminal.askInt("\nHey, What do you want to cook? \n\n [1] for appetizer \n [2] fuer main course\n [3] dessert \n\n");

        if (course == 1){
        ingredient = Terminal.askBoolean("\n so something small...do you have specific ingredients you want to use? true/false? "); 
        }
        if (course == 2) {
        ingredient = Terminal.askBoolean("\n So something serious...do you have specific ingredients you want to use? true/false? "); 
        } 
        if (course == 3) {
        ingredient = Terminal.askBoolean("\n something sweet...do you have specific ingredients you want to use? true/false? "); 
        }

         return ingredient;

    }


    public static void generating(String[] ingredients, String[] meal){

            for(int i=0; i<ingredients.length; i++){    
                for(int j=0; j<meal.length; j++){
                    if(ingredients[i] == meal[j]){
                        System.out.println(" Match found. What about "+meal[0]);
                        break;      
                    }   
                    else{
                        System.out.println("Didn't find a fitting recipe");
                    }
                }
            }
    }
}

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire