2016-11-18 167 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,則不要測試 –