Reputation: 23
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;
}
}
Upvotes: 0
Views: 1516
Reputation: 15
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.
Upvotes: 0
Reputation: 396
Always use .equals to compare strings because == operator only compares references rather than data
Upvotes: 0
Reputation: 40500
keywords[index] == choice1
...
This is the problem. Use .equals()
function to compare strings, not ==
keywords[index].equals(choice1)
etc.
Upvotes: 2