2016-11-18 158 views
-1
public static String[] wordArray(int wordAmount){ 
    String[] words = new String[wordAmount]; 
    Scanner input = new Scanner(System.in); 
    for(int i = 0; i < wordAmount; i++){ 
     System.out.print("Enter a word: "); 
     words[i] = input.nextLine(); 
     for(int j = 0; j < wordAmount; i++){ 
      if(words[i].contains(words[j]) & (i != j)){ /////////////////// 
       System.out.print("The word you entered has already been entered, enter a new word: "); 
       words[i] = input.nextLine(); 
      } 
     } 
     while(words[i].length() > 10 | words[i].length() <= 1 | words[i].contains(" ")){ 
      System.out.print("The word you entered has been rejected, enter a new word: "); 
      words[i] = input.nextLine(); 
     } 
    } 
    return words; 
} 

带有所有斜杠的代码行不起作用(只是在那里指出哪些斜线表示我有问题)im,不知道为什么它不工作,我重新安排了很多次的代码,到目前为止没有任何工作。如果有人知道为什么它不工作,我将不胜感激解决这个问题。如果声明条件不起作用

错误消息:异常在线程“主”显示java.lang.NullPointerException

+0

嗨德文,我建议你也哪种语言你的工作在让更多的人来帮助你的标签。 – micstr

+0

@aidan该链接适用于C++ –

回答

-1

问题是与空指针相关联。您正尝试接收不存在的字符串中的数据。问题定义很简单,解决方案非常简单,因此您需要控制for循环中字符串的长度。请再次检查您的代码。

编辑

看看你的for循环你是不是递增j变量中,你只是增加了我的变量,但j是始终为零,因此在我的变量总是循环递增,这样你就面临着这样的空指针异常。

EDIT2

public static String[] wordArray(int wordAmount){ 
      String[] words = new String[wordAmount]; 
      Scanner input = new Scanner(System.in); 
      for(int i = 0; i < wordAmount; i++){ 
       System.out.print("Enter a word: "); 
       words[i] = input.nextLine(); 
       for(int j = 0; j < wordAmount; j++){ 
        //boolean tt = words[i].contains("selam"); 
        if(j == 1) break; 
        System.out.println(words[i].contains(words[j])); 
        //System.out.println(tt); 
        if(words[i].contains(words[j]) & (i != j)){ /////////////////// 
         System.out.print("The word you entered has already been entered, enter a new word: "); 
         words[i] = input.nextLine(); 
        } 
       } 
       while(words[i].length() > 10 | words[i].length() <= 1 | words[i].contains(" ")){ 
        System.out.print("The word you entered has been rejected, enter a new word: "); 
        words[i] = input.nextLine(); 
       } 
      } 
      return words; 
     } 

试试这个,看看到底是怎么回事。 。 。 您需要通过stringobject.length()获取字符串长度的主要问题。那么你需要控制它,如果我写上面的块。所以问题将会消失。

EDIT3

更明确的答案对你

public static String[] wordArray(int wordAmount){ 



      String[] words = new String[wordAmount]; 
      Scanner input = new Scanner(System.in); 
      for(int i = 0; i < wordAmount; i++){ 
       System.out.print("Enter a word: "); 
       words[i] = input.nextLine(); 
       int len = i; 
       for(int j = 0; j < wordAmount; j++){ 
        //boolean tt = words[i].contains("selam"); 
        if(j == i) break; 
        System.out.println(words[i].contains(words[j])); 
        //System.out.println(tt); 
        if(words[i].contains(words[j]) & (i != j)){ /////////////////// 
         System.out.print("The word you entered has already been entered, enter a new word: "); 
         words[i] = input.nextLine(); 
        } 
       } 
       while(words[i].length() > 10 | words[i].length() <= 1 | words[i].contains(" ")){ 
        System.out.print("The word you entered has been rejected, enter a new word: "); 
        words[i] = input.nextLine(); 
       } 
      } 
      return words; 
     } 
+0

谢谢您将我的注意力带到了这里,这是我没有意识到的一个错误。但尽管如此,我仍然得到错误。 –

+0

请参阅https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence) –

+0

如果'words [j]'为null,则不要测试 –