0

I'm having a strange issue. I'm trying to pass a number of String variables denoting a user's dislikes to a predefined Java method that works by comparing these dislikes to the key ingredients stored as a String array in a Recipe object array.

The method works fine when I hard-code a dislike, such as "Beef", but when I assign the dislikes to an instance String variable kw1 using user1.getDislikes(0), the method does not perform correctly - it returns recipes that have "Beef" as a keyword, when it shouldn't.

I know the String is being passed and assigned correctly as I used a Toast to display kw1 upon returning valid results.

I've tried adding toString() in numerous places as IntelliJ was being picky about it earlier, despite claiming it is redundant, but it hasn't worked here.

Here's the section I'm having difficulty with:

if ((SetRecipes.recipes[index].searchkeywords2(kw1, kw2, kw3))) //Not working unless words (e.g. "Beef") are hardcoded for some reason. kw1 variable being assigned correctly, as shown by Toast.
         {
            temp[validRecipe] = index;

            validRecipe++;
         } //if

The full code can be found below. Any help is greatly appreciated!

public class SuggestResult extends Activity
{

   String kw1, kw2, kw3;

   static TextView [] recipeText = new TextView[8];

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.suggest_results);
      User user1 = (User)getIntent().getSerializableExtra("user1");

      kw1 = user1.getDislikes(0).toString();
      kw2 = user1.getDislikes(1).toString();
      kw3 = user1.getDislikes(2).toString();

      /*
      kw1 = "null";
      kw2 = "null";
      kw3 = "null";
      */

      recipeText[0] = (TextView)findViewById(R.id.recipeSuggestText1);
      recipeText[1] = (TextView)findViewById(R.id.recipeSuggestText2);
      recipeText[2] = (TextView)findViewById(R.id.recipeSuggestText3);
      recipeText[3] = (TextView)findViewById(R.id.recipeSuggestText4);
      recipeText[4] = (TextView)findViewById(R.id.recipeSuggestText5);
      recipeText[5] = (TextView)findViewById(R.id.recipeSuggestText6);

      final int MAXRECIPES = 7;
      final int MAXTEXTFIELDS = 6;
      int[] temp = new int[MAXRECIPES];
      int validRecipe = 0;

      SetRecipes.setArray();

      for (int index = 0; index < MAXRECIPES; index++)
      {


         if ((SetRecipes.recipes[index].searchkeywords2(kw1, kw2, kw3))) //Not working unless words (e.g. "Beef") are hardcoded for some reason. kw1 variable being assigned correctly, as shown by Toast.
         {
            temp[validRecipe] = index;

            validRecipe++;
         } //if
      }

      if (validRecipe == 0)
      {
         Context context = getApplicationContext();
         CharSequence text = "No valid recipes found!";
         int duration = Toast.LENGTH_SHORT;
         Toast toast = Toast.makeText(context, text, duration);
         toast.show();
      }

      for (int index3 = 0; (index3 < validRecipe) && (index3 < MAXTEXTFIELDS); index3++)
      {
         recipeText[index3].setText((SetRecipes.recipes[temp[index3]].getName()).toString());

      }


      Context context = getApplicationContext();
      CharSequence text2 = kw1;
      int duration = Toast.LENGTH_SHORT;
      Toast toast = Toast.makeText(context, text2, duration);
      toast.show();


     }

}

searchkeywords2 method:

public boolean searchkeywords2(String choice1,String choice2, String choice3)
    {
        int ingredientsPresent = 0;


        for (int index = 0; index < keywords.length; index++)
        {
            if ((keywords[index] == choice1) || (keywords[index] == choice2) || (keywords[index] == choice3))
            {
                ingredientsPresent++;
            }
        }
        if (ingredientsPresent == 0)
        {
            return true;
        } else
        {
            return false;
        }


    }
8
  • "not working" ... any chance to be more specific?
    – Tom
    Dec 7, 2014 at 1:41
  • Apologies, I've updated the explanation. The method is filtering recipes containing the keyword "Beef" when I pass "Beef" as a parameter, but not filtering recipes with beef when I pass kw1 as a parameter. Dec 7, 2014 at 1:44
  • So the interesting method is searchkeywords2? Can you add it to the question?
    – Tom
    Dec 7, 2014 at 1:45
  • Can you do a System.printf(kw*) ... a verify they are what you expect ... its not surprising that user1 is the result of a cast, and you are having a problem with something derived from that
    – nPn
    Dec 7, 2014 at 1:48
  • Sure, it's been added. Thanks for the help! Dec 7, 2014 at 1:49

3 Answers 3

2

keywords[index] == choice1 ...

This is the problem. Use .equals() function to compare strings, not ==

keywords[index].equals(choice1) etc.

5
  • seems like the likely cause
    – nPn
    Dec 7, 2014 at 1:55
  • @RuairiMcGowan I guess because keywords[index] is null (i.e. you have null elements in this array). A quick fix can be choice1.equals(keywords[index]), but you should check why you have null elements in that array and if they are supposed to be there.
    – Tom
    Dec 7, 2014 at 2:05
  • It shouldn't be null as it was working with the hard-coded strings as a parameter, BUT it is now working fully using if (choice1.equals(keywords[index]) || choice2.equals(keywords[index]) || choice3.equals(keywords[index])) Thanks to everyone for their help! Dec 7, 2014 at 2:17
  • if keywords[index] is null, indeed, keywords[index] == choice1 would work, but keywords[index].equals(choice1) would crash. Why do you have nulls in keywords? Does not sound like it is intended ...
    – Dima
    Dec 7, 2014 at 4:00
  • Dima, you are correct, the keywords array was declared with 6 elements despite us using only 3 for our current recipes, this means keyword.length is 6 and the FOR loop tries to access the 4th element in the keyword array, which is null. Thanks for the explanation! Dec 7, 2014 at 19:02
0

Always use .equals to compare strings because == operator only compares references rather than data

0

When we use == operator, it checks if the objects point to the same location on the memory but the .equals on the other hand apart from checking if the objects point to the same location also checks for the equality of the object content in the memory location, thus providing double check. You can also override the equals class to perform other checks. So, always use the .equals to check equality of 2 objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.