why doesn't this code work? (charAt)

edited December 2016 in Questions about Code

'l' is a variable being used. I want this code to take the number at location 'l' but I get the message "String Index Out Of Bounds Exception: String index out of range: 4" even though the value of l is less than 4.

if (saved.charAt(l) == 1)

Tagged:

Answers

  • Can you prove that? Add the following debug

    println("[" + saved + "]");
    println("l:" + l);
    

    1 is and of thing to compare a char to. Do you mean '1'?

  • edited December 2016

    @koogs i added what you said to but i still get the same problem. in this case saved = 1001 and 'l' = the length of the string (4).

         println("[" + saved + "]");
                println("l:" + l);
              if (saved.charAt(l) == 1) 
    

    Captureprocessing

  • Answer ✓

    @koogs i actually figured it out. I forgot that the first character is 0 not 1. Thanks for your help!

Sign In or Register to comment.