java - 将字符串变量传递给 Java 方法不起作用,但硬编码字符串起作用?

标签 java android intellij-idea

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

当我硬编码不喜欢的内容(例如“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();


     }

}

搜索关键字2方法:

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;
        }


    }

最佳答案

关键字[索引] == choice1 ...

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

关键字[index].equals(choice1)

关于java - 将字符串变量传递给 Java 方法不起作用,但硬编码字符串起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27338614/

相关文章:

android - 新的 Android Eclipse 版本无法进入工作区

intellij-idea - Intellij IDEA 无法使用 Play 框架解析符号

android - 是否可以从在应用程序市场中下载的应用程序获取Eclipse中的.java,.xml和图像资源 View ?

android - onTouchListener 覆盖按钮 View 上的自定义背景?

plugins - JetBrains IDE 插件库的 URL 是什么?

IntelliJ IDEA中Android开发导调用脑死机

java - 无法使用 java 进程的 gdb 核心反汇编 $PC

java - 自定义 ListView 适配器抛出 UnsupportedOperationException

Sin 和 ToRadians 的 Java 奇怪行为

java - 在 Java 中将 JSON 解析为字符串时遇到问题