2014-12-07 48 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個對象的相等性。

相關問題