1

我不明白为什么这不起作用?我正在尝试从字符串中的数组中查找关键字,并在控制台中打印数组的索引号。我已经尝试了带有和不带有布尔值“true”的“if”语句

public class Testing 
{
    public static void main(String[] args)
    {
    String[] keywords = new String[5];
    keywords[0] = "boat";
    keywords[1] = "car";
    String myString = "the banana car";

          for(int a = 0; a <= keywords.length; ++a)

          {

             if(myString.contains(keywords[a])== true)
                {

                System.out.println(myString.indexOf(keywords[a]));
                }
             else
                {
                System.out.println("Those keywords are not in that string"); 
                }

           }
     }
}
4

6 回答 6

4
String[] keywords = new String[5];////////////here you have array with 5 element
    keywords[0] = "boat";
    keywords[1] = "car";
//////////you just full index 0 and index 1

然后你 for 循环关键字的长度

for (int a = 0; a <keywords.length; a++) 
//////keywords.length=5 , index 0 and index 1 have a value but the other index is empty 

所以 :

  • 您需要填写关键字数组:

     keywords[0] = "boat";
     keywords[1] = "car";
     keywords[2] = //////////;
     keywords[3] = //////////;
     keywords[4] = ///////;
    
  • 或使长度= 2:

    String[] keywords = new String[2];
    
于 2013-03-31T18:52:49.853 回答
1

您已将 String 数组声明为包含 5 个元素,因此缺少 3 个额外元素

String[] keywords = new String[5];
    keywords[0] = "boat";
    keywords[1] = "car";
于 2013-03-31T18:53:55.547 回答
1

a <= keywords.length应该是a < keywords.length,如果您声明它的大小为5以具有5 个元素(从 0 到 4),则应该填充该数组。

数组在Java中为零,所以如果你有长度为n的数组,那么索引是从0n-1(总和为n

另一个重要的事情:

if(myString.contains(keywords[a])== true)中,== true是多余的,因为myString.contains(keywords[a])返回truefalse,在这里您只想检查它。因此,删除== true.

于 2013-03-31T18:54:01.610 回答
1

你还没有初始化你的所有元素keywords,因此你会得到一个NullPointerException

如果您只想检查初始化的前两个元素,请将 for 循环更改为以下内容:

for (int a = 0; a <= 1; ++a)
于 2013-03-31T18:54:30.757 回答
1

这个程序:

public class Testing {

   public static void main( String[] args ) {
      String[] keywords = new String[]{ "boat", "car" }; // Only 2 not 5
      String myString = "the banana car";
      for( String keyword : keywords ) {
         int index = myString.indexOf( keyword );
         if( index > -1 ) {
            System.out.println(
               "Keyword '" + keyword + "' is in the string '" +
               myString + "' at position " + index ); 
         }
         else
         {
            System.out.println( "Keyword '" + keyword +
               "' is not in the string '" + myString + "'" ); 
         }
      }
   }
}

输出:

Keyword 'boat' is not in the string 'the banana car'
Keyword 'car' is in the string 'the banana car' at position 11
于 2013-03-31T18:57:38.783 回答
0
public static void main(String[] args) {
    String[] keywords = {"boat", "car"};
    String myString = "the banana car";
    for(int i = 0; i < keywords.length; i++) {
        System.out.println(myString.contains(keywords[i]) ? myString.indexOf(keywords[i]) : "Those keywords are not in that string");

    }
}

..应该管用

于 2013-03-31T18:55:09.980 回答