繁体   English   中英

将String变量传递给Java方法不起作用,但是硬编码的字符串起作用了吗?

[英]Passing String variables to Java method not working, but hard-coded string working?

我有一个奇怪的问题。 我正在尝试将许多表示用户不喜欢的String变量传递给预定义的Java方法,该方法通过将这些不喜欢与存储为Recipe对象数组中的String数组的关键成分进行比较来工作。

当我硬编码不喜欢的内容(例如“牛肉”)时,该方法运行良好,但是当我使用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;
        }


    }

keywords[index] == choice1 ...

这就是问题。 使用.equals()函数比较字符串,而不是==

keywords[index].equals(choice1)等。

始终使用.equals来比较字符串,因为==运算符仅比较引用而不是数据

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM