0

I am trying to figure out "what 5-digit number when multiplied by 4 gives you its reverse?" using this code but I get error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 5 at java.lang.String.charAt(String.java:658) at     
Digits.main(Digits.java:15)

I would like to figure out (have someone explain) why this is happening. I would like to keep my charAt in my code and not use StringBuilder (StringBuilder.reverse()) if that is possible.

public class Digits{
  public static void main(String[] args) {
    int n = 0;
    int b = 0;
    String number = "";
    String backwards = "";

    for (int x = 9999; x <= 99999 ; x++ ) {
      n = x;
      b = x * 4;
      number = Integer.toString(n);
      backwards = Integer.toString(b);

      if ( number.charAt(0) == backwards.charAt(4) && number.charAt(1) == backwards.charAt(3)
      && number.charAt(2) == backwards.charAt(2) && number.charAt(3) == backwards.charAt(1)
      && number.charAt(4) == backwards.charAt(0)) {
        System.out.println(n);
        break;
      }
    }

Thanks

4
  • Possible duplicate of stackoverflow.com/questions/31375307/… Commented Jul 13, 2015 at 5:52
  • This exception is not possible for this code. you donot have have any chatAt(5).Check again . Commented Jul 13, 2015 at 5:54
  • you code doesnt give me any index error as explained by you Commented Jul 13, 2015 at 5:55
  • 2
    Okay so I just realized that the code works fine. For some reason my compiler was not working and kept sending out an error. Commented Jul 13, 2015 at 5:59

2 Answers 2

1

The code runs without an exception, the code tested is given below:

public class Digits {

    public static void main(String[] args) {
        int n;
        n = 0;
        int b;
        b = 0;
        String number;
        number = "";


    String backwards;
        backwards = "";

        for (int x = 9999; x <= 99999; x++) {
            n = x;
            b = x * 4;
            number = Integer.toString(n);
            backwards = Integer.toString(b);

            if (number.charAt(0) == backwards.charAt(4) && number.charAt(1) == backwards.charAt(3)
                    && number.charAt(2) == backwards.charAt(2) && number.charAt(3) == backwards.charAt(1)
                    && number.charAt(4) == backwards.charAt(0)) {
                System.out.println(n);
                break;
            }
        }
    }
}

Ouput of this code is 21978

Sign up to request clarification or add additional context in comments.

Comments

0

This exception is not possible for this code. you donot have have any chatAt(5).Check again .

3 Comments

This isn't an answer, it is a comment.
@AndyTurner do you want me to make something up .
@shekharsuman this was the comment until people posted answers including some with up votes. However this was the only possibility as OP has confirmed that question was infect wrong. Due to answers OP can not delete the questions now and my answer is the only valid explanation. Other answers should have been down voted since this was not a real problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.