我有一个奇怪的问题。我尝试传递一些String变量,表示用户不喜欢预定义的Java方法,该方法通过将这些不喜欢与在Recipe对象数组中存储为String数组的关键成分进行比较而起作用。
当我硬编码不喜欢的方法(例如" Beef")时,该方法正常工作,但是当我使用user1.getDislikes(0)将不喜欢分配给实例字符串变量kw1时,该方法不会正确执行 - 它返回有"牛肉"作为关键字,当它不应该时。
我知道正在传递和正确分配字符串,因为我使用Toast在返回有效结果时显示kw1。
我尝试在很多地方添加toString(),因为IntelliJ之前对它很挑剔,尽管声称它是多余的,但它在这里没有用。
以下是我遇到困难的部分:
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
完整代码可以在下面找到。非常感谢任何帮助!
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方法:
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;
}
}
答案 0 :(得分:2)
keywords[index] == choice1
...
这就是问题所在。使用.equals()
函数比较字符串,而不是==
keywords[index].equals(choice1)
等。
答案 1 :(得分:0)
始终使用.equals来比较字符串,因为==运算符只比较引用而不是数据
答案 2 :(得分:0)
当我们使用==运算符时,它会检查对象是否指向内存中的相同位置,但另一方面.equals除了检查对象是否指向同一位置之外还检查对象是否相等内容位于内存位置,从而提供双重检查。您还可以覆盖equals类以执行其他检查。 因此,始终使用.equals来检查2个对象的相等性。