Heya! I'm having trouble converting a .txt file to ints and storing them in an array. All variables not seen have been properly initialized. Initially, I was getting an error message similar to the one below, but instead, it was the � (unidentified character) you see in one of the .replaceAll statements. I think the issue is happening during the code's attempt at parsing the string, and for some reason, it's saying that the null space can't be converted into an int, when it should just skip over it (it's not even a space, it's just null). Any clue as to what I should do would be greatly appreciated. Thanks!
Here's the error:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(Num berFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:646)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at tile.TileManager.loadMap(TileManager.java:86)
at tile.TileManager.<init>(TileManager.java:30)
at main.GamePanel.<init>(GamePanel.java:41)
at main.Main.main(Main.java:20)
"at tile.TileManager.loadMap(TileManager.java:86)" is referencing this line:
nums[col] = Integer.parseInt(String.valueOf(line.charAt(col))) ;
Anything errors below this one are irrelevant, as it's just tracing how the class got called.
public void loadMap() { //try { InputStream is = getClass().getResourceAsStream("/maps/map0001.txt"); //imports the text file Scanner scnr = new Scanner(is); //lets us read the contents of the text file int col = 0; int row = 0; while(row < gp.maxScreenRow) { //loops through all rows coordinates while(scnr.hasNextLine()) { //checks if the scanner has anything to read, if so, it reads that line, stores it in mapTileNum and increments row String line = scnr.nextLine();//stores a scanned line in the String "line" line = line.replaceAll(" ","");//removes all spaces line = line.replaceAll("�","");//removes all unknown characters if(line.equals("")) { continue; } //String[] numbers = new String[gp.maxScreenCol];//creates a new String array that will store all of the numbers in a given row int[] nums = new int[gp.maxScreenCol];//all of the Strings in numbers will be converted to ints and stored here while(col < line.length()) {//goes through all characters (actually numbers, not yet parsed) of line nums[col] = Integer.parseInt(String.valueOf(line.charAt(col))); //uses col to get a specific value from the map, and .parseInt changes it from a string to an Integer mapTileNum[col][row] = nums[col]; //stores the extracted value in the equivalent place in a 2d array col++; } /*for(int i = 0; i < numbers.length; i++) { String replacement = numbers[i].replace(" ", ""); numbers[i] = replacement; }*/ row++; } } scnr.close(); /*}catch(IOException e) { e.printStackTrace(); }*/ }