StringIndexOutOfBoundsException String index out of range: 0

The problem occurs when line is empty (e.g. ""). Then it doesn’t have a character at index 0, hence your error.

To fix, you can check the length of line before you use charAt:

System.out.println(line);
char x;
if (line.length() < 1) {
   System.out.println("next line is empty");
} else {
    x = line.charAt(0);

    while((line.charAt(0)!='/')&&(Character.isWhitespace(x)==false))
    {
       line = inputFile.nextLine();
       if (line.length() < 1) {
          System.out.println("next line is empty");
          break;
       }
       x = line.charAt(0);
       System.out.println(line);
       System.out.println(x);
    }
}

Leave a Comment