2014-04-28 2 views
0

안녕 얘들 아 나는 자바에 대해 매우 익숙하지 않다. 왜 누군가가 내 배열의 시작 부분에 무작위 null을 얻었는지 설명 할 수 있는지 궁금하다. [null, r, b] , O, e, r, t] 왜 그렇게하는지 모르겠다! 어떤 도움이 그것을 없애 버리는 것은 굉장 할 것입니다!.split()을 사용할 때 null

public static String [] Wordarray(Scanner input) 
    { 
     String [] temp = {}; 
     String word = ""; 
     do{ 
     System.out.println("Enter a word you'd like to be guess"); 
     word = input.nextLine().toLowerCase(); 
     if(word.length()<5) 
      System.out.println("Error...."); 
     }while(word.length()<5); 
     String [] words = word.split(""); 

     return words; 
} 

미리 감사드립니다.

example input to output Enter a word you'd like to be guess 
beees 

The word contains 6letters. 
Amount of chances left 7 

avaliable letters : [, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z] 
Enter a letter : 
b 
[] 
[-, b, -, -, -, -] 
[null, b, null, null, null, null] 
Amount of chances left 7 

avaliable letters : [, a, , c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z] 
Enter a letter : 
e 
[-, b, -, -, -, -] 
[-, -, e, e, e, -] 
[null, b, e, e, e, null] 
Amount of chances left 7 

Enter a letter : 
s 
[-, -, e, e, e, -] 
[-, -, -, -, -, s] 
[null, b, e, e, e, s] 
Amount of chances left 6 
+0

은 우리에게 예를 들어 입력을 지정합니다. –

+0

배열에 첫 번째 요소로 'null'이 포함 된 배열도 표시하십시오. –

+2

왜'char [] words = word.toCharArray();' –

답변

3

첫 번째 문자 전에 Empty String이 있기 때문에. 말 $는 다음 문자열 실제로, 빈 문자열 :

rboert -> $r$b$o$e$r$t 

그래서 출력은 다음과 같습니다

[null,r,b,o,e,r,t] 

은 기본적으로는 split에서 정규식으로 빈 문자열을 사용하는 것은 좋지 않습니다. 이 정규식을 사용하여 문자열의 문자를 분할 수 :

"abcd".split("(?<=.)"); 

출력은 다음과 같습니다

[a, b, c, d] 
+0

감사합니다 !!!!! 당신은 대단해! – Bob

관련 문제