Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串变量传递给Java方法不起作用,但硬编码字符串起作用?_Java_Android_Intellij Idea - Fatal编程技术网

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

将字符串变量传递给Java方法不起作用,但硬编码字符串起作用?,java,android,intellij-idea,Java,Android,Intellij Idea,我有一个奇怪的问题。我试图将表示用户不喜欢的一些字符串变量传递给预定义的Java方法,该方法通过将这些不喜欢与作为字符串数组存储在Recipe对象数组中的关键成分进行比较来工作 当我硬编码一个不喜欢的东西(如“牛肉”)时,该方法工作得很好,但当我使用user1.getDislikes(0)将不喜欢的东西分配给实例字符串变量kw1时,该方法执行不正确-它返回的菜谱中有“牛肉”作为关键字,而它不应该返回 我知道字符串被正确传递和分配,因为我在返回有效结果时使用Toast来显示kw1 我曾尝试在许多地

我有一个奇怪的问题。我试图将表示用户不喜欢的一些字符串变量传递给预定义的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();


     }

}
公共类SuggestResult扩展活动
{
串kw1、kw2、kw3;
静态文本视图[]recipeText=新文本视图[8];
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.建议结果);
用户user1=(用户)getIntent().getSerializableExtra(“用户1”);
kw1=user1.getDislikes(0.toString();
kw2=user1.getDislikes(1.toString();
kw3=user1.getDislikes(2.toString();
/*
kw1=“空”;
kw2=“空”;
kw3=“空”;
*/
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);
最终int MAXRECIPES=7;
最终整数MAXTEXTFIELDS=6;
int[]temp=新int[MAXRECIPES];
int validRecipe=0;
SetRecipes.setArray();
对于(int index=0;index
SearchKeywords 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;
        }


    }
公共布尔搜索关键字2(字符串选项1、字符串选项2、字符串选项3)
{
int INGREDIENTS PRESENT=0;
for(int index=0;index
关键字[索引]==choice1

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


关键字[index].equals(choice1)
等。

始终使用.equals来比较字符串,因为==运算符在使用==运算符时只比较引用而不比较数据,它检查对象是否指向内存中的相同位置,但另一方面,除了检查对象是否指向相同位置外,还检查内存位置中对象内容的相等性,从而提供双重检查。您还可以重写equals类以执行其他检查。
因此,始终使用.equals来检查两个对象的相等性。

“不工作”。。。有没有更具体的机会?抱歉,我已经更新了解释。该方法是在我将“Beef”作为参数传递时过滤包含关键字“Beef”的菜谱,而不是在我将kw1作为参数传递时过滤包含Beef的菜谱。因此,有趣的方法是
SearchKeywords 2
?你能把它加到问题上吗?你能做一个系统吗。。。a验证它们是否符合您的期望。。。毫不奇怪,user1是一个cast的结果,并且您对从中派生的东西有问题。当然,它被添加了。谢谢你的帮助!看起来很有可能cause@RuairiMcGowan我猜是因为
keywords[index]
null
(也就是说,这个数组中有
null
元素)。快速修复方法可以是
choice1.equals(关键字[index])
,但您应该检查该数组中为什么有
null
元素,以及它们是否应该存在。它不应该为null,因为它使用硬编码字符串作为参数,但现在它完全使用
if(choice1.equals(关键字[index])| |选择2.equals(关键字[索引])| |选择3.equals(关键字[索引])
感谢大家的帮助!如果
keywords[index]
null
,实际上,
keywords[index]==choice1
可以工作,但是
keywords[index].equals(choice1)
会崩溃。为什么关键字中有空值?听起来不像是有意的…迪玛,你是