2016-12-13 3 views
-1

이제 단어의 접미사를 인쇄하려고하는데 몇 가지 문제가 있습니다. StringBuffer.CharAt를 포함한 StringIndexOutOfBoundsException

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 
at java.lang.StringBuffer.charAt(Unknown Source) 
at _20161212.Launcher.compareSameThings(Launcher.java:67) 
at _20161212.Launcher.main(Launcher.java:52) 

는 ErrorCode가 있습니다 :

나는 이유를 찾기 위해 노력하고있어,하지만 나에게 어렵습니다.

public static void compareSameThings(StringBuffer[] strbuf, int index01, int index02, int count) { 
    count++; 
    if ((int) strbuf[index01].charAt(count) > (int) strbuf[index02].charAt(count)) { 
     StringBuffer temp = strbuf[index01]; 
     strbuf[index01] = strbuf[index02]; 
     strbuf[index02] = temp; 
    } else if ((int) strbuf[index01].charAt(count) == (int) strbuf[index02].charAt(count)) { 
     compareSameThings(strbuf, index01, index02, count); 
    } 
} 

for (int index01 = 0; index01 < str.length() - 1; index01++) { 
    for (int index02 = index01 + 1; index02 < str.length(); index02++) { 
     int count = 0; 
     if ((int) strbuf[index01].charAt(count) > (int) strbuf[index02].charAt(count)) { 
      StringBuffer temp = strbuf[index01]; 
       strbuf[index01] = strbuf[index02]; 
       strbuf[index02] = temp; 
     } else if (strbuf[index01].charAt(count) == strbuf[index02].charAt(count)) { 
       compareSameThings(strbuf, index01, index02, count); 
     } 
    } 
} 

당신이 나에게 이러한 문제가 나타나는 이유 이유를 말할 수 있습니까?

+0

52 번째 줄은 무엇입니까? – Thrasher

+0

@Thrasher 정말 중요하지 않습니다. 오히려 약 67 번째를 궁금해해야합니다. 어쨌든, 52 번째 줄은 아마도 두 번째 코드 조각의 마지막 줄입니다. – xenteros

+0

[디버거 란 무엇이며 어떻게 문제를 진단하는 데 도움이됩니까?] (http://stackoverflow.com/questions/25385173/what-is-a) -debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald

답변

3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1 
at java.lang.StringBuffer.charAt(Unknown Source) 
at _20161212.Launcher.compareSameThings(Launcher.java:67) 
at _20161212.Launcher.main(Launcher.java:52) 

의미 :

There was an Exception thrown. It's called StringIndexOutOfBoundsException and 
    the index which was out of bounds was 1 
it was thrown in method charAt 
in compareSameThings(Line 67th in Launcher.java) 
in main(Line 52 in Launcher.java) 

그래서 기본적으로, 당신이 인덱스 1에서 문자를 얻기 위해 시도했지만 그런 문자가 없었다. 즉, StringBuffer의 문자 수가 2 문자 미만입니다.

관련 문제