2014-12-07 80 views
0

我遇到了一个奇怪的问题。我试图将一些表示用户不喜欢的字符串变量传递给预定义的Java方法,该方法通过将这些不喜欢的内容与作为String数组存储在Recipe对象数组中的关键组件进行比较来工作。将字符串变量传递给Java方法不起作用,但硬编码的字符串正在工作?

当我硬编码厌恶,如“牛肉”,但当我使用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

“不工作”......有什么机会变得更具体? – Tom 2014-12-07 01:41:51

+0

道歉,我已经更新了解释。当我传递“牛肉”作为参数时,该方法正在过滤包含关键字“牛肉”的食谱,但是当我将kw1作为参数传递时,不会用牛肉过滤食谱。 – 2014-12-07 01:44:46

+0

那么有趣的方法是'searchkeywords2'?你可以把它添加到问题? – Tom 2014-12-07 01:45:49

回答

2

keywords[index] == choice1 ...

这就是问题所在。使用.equals()功能比较字符串,而不是==

keywords[index].equals(choice1)

+0

似乎是可能的原因 – nPn 2014-12-07 01:55:20

+0

似乎可能,但现在我得到一个NullPointerException由于某种原因.. – 2014-12-07 02:00:20

+0

@RuairiMcGowan我猜是因为'keywords [index]'是'null'(即你有'null'元素在这个数组)。一个快速修复可以是'choice1.equals(keywords [index])',但是你应该检查为什么你在这个数组中有'null'元素,并且它们是否应该在那里。 – Tom 2014-12-07 02:05:06

0

始终使用.equals比较字符串,因为当我们使用==操作符只比较引用,而不是数据

0

==操作符,它检查对象是否指向内存中的相同位置,但除了检查对象是否指向相同位置之外,另一方面.equals还检查内存位置中对象内容是否相等,从而提供双重检查。您也可以重写equals类来执行其他检查。 因此,请始终使用.equals来检查2个对象的相等性。

相关问题