-1

I'm now trying to print word's suffixes, and there are some problems.

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)

This is the ErrorCode:

and I'm trying to find the reason, but it's difficult to me.

this

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);
    }
}

and this

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);
        }
    }
}

Can you tell me the reasons why these problems are appearing?

Ankita Shah
  • 1,866
  • 16
  • 31
pyuni
  • 7
  • 2
  • Which is you line 52? – Rcordoval Dec 13 '16 at 06:30
  • @Thrasher it doesn't really matter. You should rather wonder about 67th. Anyway, Line 52 is probably the last line in the second snippet – xenteros Dec 13 '16 at 06:34
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Dec 13 '16 at 08:01

1 Answers1

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)

means:

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)

So basically, you tried to get character at index 1 but there was no such character. This means, that your StringBuffer has less than 2 characters.

xenteros
  • 15,586
  • 12
  • 56
  • 91