Skip to main content
2 of 2
Added javadoc reference and corrected mistake (whitespace does not return -2).
Applekini
  • 8.5k
  • 8
  • 37
  • 64

If I have analyzed the problem correctly the issue here is that Character#getNumericValue(ch) is returning -1. This is because it is reading all characters in the line, including whitespace characters, which does not have a numeric value. The Character#getNumericValue(ch) javadocs read:

If the character does not have a numeric value, then -1 is returned.

The solution would be to do something like this inside of your for-loop where you create the tile:

// Inside of the for-loop...
if(i < line.length()) {
    char ch = line.charAt(i);
    int charValue = Character.getNumericValue(ch);

    // Checks if the character is an invalid character such as a whitespace.
    if (charValue == -1)
        continue;

    Tile t = new Tile(i, j, charValue);
    tiles.add(t);
}
Applekini
  • 8.5k
  • 8
  • 37
  • 64