Ask coding questions

← Back to all posts
StringIndexOutOfBoundsException
ProgrammerAI (4)

Help guys. I keep getting a StringIndexOutOfBoundsException no matter what i do

Commentshotnewtop
mwilki7 (280)

The error happens at line 59 which means the while loop was entered when it shouldn't have.
inputChar = inputLine.charAt(i);

Usually the culprit of these sorts of things are the while/for/if conditions.

while (inputLine = in.readLine()) != null || i < inputLine.length()

Logical OR statements only require one of the statements to be true. I think this problem can be solved by making the conditional more restrictive.

So just change
|| to &&

(inputLine = in.readLine()) != null || i < inputLine.length()

to

(inputLine = in.readLine()) != null && i < inputLine.length()
ProgrammerAI (4)

@mwilki7 i tried and now it doesn't add the file and also cuts of to short