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

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Vayelin
  • 75
  • 1
  • 2
  • 10
  • Possible duplicate of http://stackoverflow.com/questions/31375307/java-charat-string-index-out-of-range-5 – SatyaTNV Jul 13 '15 at 05:52
  • This exception is not possible for this code. you donot have have any `chatAt(5)`.Check again . – Shahzeb Jul 13 '15 at 05:54
  • you [code](http://ideone.com/Kwm2FC) doesnt give me any index error as explained by you – singhakash Jul 13 '15 at 05: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. – Vayelin Jul 13 '15 at 05:59

2 Answers2

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

pelumi
  • 1,530
  • 12
  • 21
Ajmal Muhammad
  • 685
  • 7
  • 25
0

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

Shahzeb
  • 4,745
  • 4
  • 27
  • 40
  • This isn't an answer, it is a comment. – Andy Turner Jul 13 '15 at 06:34
  • @AndyTurner do you want me to make something up . – Shahzeb Jul 13 '15 at 10:44
  • @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. – Shahzeb Jul 17 '15 at 22:32