質問

So I need to write a method which allows me to find the next number without using anything but String and StringBuffer classes (so no parse or whatsoever).

when I run my code everything seems to work fine except for the last input (String k). It causes an outofbounds error in the while loop. But I don't see how It could do that because since my boolean should be false (I think because it only consists out of nine's) it shouldn't enter te loop in the first place.

public class Palindroom {
public static void main(String[] args) {
    // TODO Auto-generated method stub
      String s="347"
            + "";           System.out.println("s "+increment(s));
       String p="199919";System.out.println(" p "+increment(p));
       String d="199999";System.out.println( " d "+increment(d));
       String k ="999999";System.out.println(" k "+increment(k));

}

static String increment (String s) {
    StringBuffer sb = new StringBuffer (s);
    char laatst = sb.charAt(sb.length()-1);
    int laatste = sb.length()-1;

    if(laatst == '9') {
        int a = 1;
        boolean nine = false;
        for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) != 9 ) {
                nine = true;

            } else nine = false;
        }
        if (nine == true) {
            while (sb.charAt(sb.length()- a) == '9'){

                sb.setCharAt(sb.length()- a, '0' );

                a++;
            }
            sb.setCharAt(sb.length()-a, ((char)((sb.charAt(sb.length()-a)+1))));

        } else if( nine == false) {
            System.out.println("negen "+nine);
            sb.insert(0, '1');
            for (int i = 0; i < sb.length(); i++) {
                if(sb.charAt(i)=='9') {
                    sb.setCharAt(i, '0');
                }
            }
        }

    } else {
        sb.setCharAt(laatste,(char) (laatst+1) ); 
    }

    return sb.toString();
  }
}
役に立ちましたか?

解決

EDIT: based on the comments :) . Correct your logic for true and false.

 for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) == '9' ) {
                nine = true;

            } 
            else 
           {
               nine = false;
               break;
           }

        }

also you are getting an out of bounds exception because

while (sb.charAt(sb.length()- a) == '9'){

                sb.setCharAt(sb.length()- a, '0' );

                a++;
            }

after the fifth iteration sb.length()- a will be negative when the control comes to check the condition.

you are not getting the same error in other cases because the control never goes to the while loop (as nine == false)

OLD OBSERVATION

in

if(laatst == '9') {
        int a = 1;
        boolean nine = false;
        for (int i = 0; i < sb.length(); i++) {
            if (sb.charAt(i) != 9 ) {
                nine = true;

            } else nine = false;
        }

you are doing

if (sb.charAt(i) != 9 ) {
                nine = true;

            } else nine = false;

instead do, if you want to compare against the character 9,

if (sb.charAt(i) != '9' ) {
            nine = true;

        } else nine = false;

otherwise you are just comparing the ASCII value of the charAt(i) and so your boolean will only be false, if charAt(i) has ascii value 9

他のヒント

The problem is that when your string consists only of 9s, your while loop causes a to exceed sb.length(). Then, you end up trying to access the character at place -1 of sb.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top