2016-11-18 1 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; 
} 

코드 뒤에있는 모든 슬래시가있는 코드 줄이 작동하지 않습니다 (문제가있는 줄을 나타 내기 위해 슬래시를 넣으십시오). 나는 여러 번 코드를 재배치했으며 지금까지는 아무 것도 작동하지 않았다. 누구나 왜 작동하지 않는지 알면이 문제에 대한 해결책을 매우 높이 평가할 것입니다.문 조건이 작동하지 않는 경우

오류 메시지 : 스레드에서 예외 "주"java.lang.NullPointerException이

+0

안녕 데빈, 나는 또한 당신을 도울 수있는 더 많은 사람을 얻기 위해 작업하고 언어 태그를 제안한다. – micstr

+0

@aidan 링크는 C++ –

답변

-1

이 문제는 널 포인터와 연결되어 있습니다. 존재하지 않는 문자열로 데이터를 받으려고합니다. 문제 정의는 간단하고 솔루션은 매우 간단하므로 for 루프에서 문자열의 길이를 제어해야합니다. 코드를 다시 확인하십시오. 이 널을 직면하고 있으므로 루프 방금 변수 i를 증가 제 j 변수를 증가시키지 않지만, j는 항상 0 귀하의

편집

봐 그래서 내가 변수는 항상 루프로 증가 포인터 예외.

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 인 경우 테스트하지 않음 –

관련 문제