【问题标题】:Passing String variables to Java method not working, but hard-coded string working?将字符串变量传递给 Java 方法不起作用,但硬编码字符串起作用?
【发布时间】:2015-02-04 23:08:59
【问题描述】:

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

当我硬编码不喜欢时,该方法工作正常,例如“牛肉”,但是当我使用 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;
        }


    }

【问题讨论】:

  • “不工作”......有机会更具体吗?
  • 抱歉,我已经更新了解释。该方法在我将“牛肉”作为参数传递时过滤包含关键字“牛肉”的食谱,但在我将 kw1 作为参数传递时不过滤包含牛肉的食谱。
  • 那么有趣的方法是searchkeywords2?您可以将其添加到问题中吗?
  • 你能做一个 System.printf(kw*) ... 验证它们是你所期望的...... user1 是强制转换的结果并不奇怪,你遇到了问题从那得到的东西
  • 当然,已添加。感谢您的帮助!

标签: java android intellij-idea


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      keywords[index] == choice1 ...

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

      keywords[index].equals(choice1)

      【讨论】:

      • 似乎是可能的原因
      • @RuairiMcGowan 我猜是因为keywords[index]null(即你在这个数组中有null 元素)。快速修复可以是choice1.equals(keywords[index]),但您应该检查为什么该数组中有null 元素以及它们是否应该存在。
      • 不应该为空,因为它使用硬编码字符串作为参数,但它现在完全使用if (choice1.equals(keywords[index]) || choice2.equals(keywords[index]) || choice3.equals(keywords[index])) 感谢大家的帮助!
      • 如果keywords[index]null,确实,keywords[index] == choice1 可以工作,但keywords[index].equals(choice1) 会崩溃。为什么关键字中有空值?听起来不像是有意...
      • Dima,你是对的,关键字数组声明了 6 个元素,尽管我们当前的配方只使用了 3 个元素,这意味着关键字长度为 6,FOR 循环尝试访问第 4 个元素关键字数组,为空。谢谢你的解释!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多