简体   繁体   中英

Scan in 2D array txt file in Java

When I scan in my 2d array text file, I get an exception error if I don't have a number in the first line. My text file is as follows:

0
8, 3, 5, 4, 1, 6, 9, 2, 7
2, 9, 6, 8, 5, 7, 4, 3, 1
4, 1, 7, 2, 9, 3, 6, 5, 8
5, 6, 9, 1, 3, 4, 7, 8, 2
1, 2, 3, 6, 7, 8, 5, 4, 9
7, 4, 8, 5, 2, 9, 1, 6, 3
6, 5, 2, 7, 8, 1, 3, 9, 4
9, 8, 1, 3, 4, 5, 2, 7, 6
3, 7, 4, 9, 6, 2, 8, 1, 5

The zero at the top, should not be there. However, I have found that if I take it out, my code doesn't work. The number doesn't have to be a zero, it works with any number as long as there is something there. Here is my code:

public static void main(String[] args) throws FileNotFoundException {
    String fileName = "Sudoku2d.txt";
    Scanner inputStream = null;
    System.out.println("The file " + fileName + "\ncontains the following lines:\n");
    inputStream = new Scanner(new File("C:\\Users\\username\\Documents\\NetBeansProjects\\Sudoku\\Sudoku2d.txt"));
    String line = inputStream.nextLine();
    int[][] puzzel = new int[9][9];
    for (int row = 0; row < 9; row++) {
        for (int column = 0; column < 9; column++) {
            puzzel[row][column] = Integer.parseInt(line);
        }
    }

    while (inputStream.hasNextLine()) {
        line = inputStream.nextLine();
        System.out.println(line);
    }
    inputStream.close();

}

Here is my output with the zero at the top of the text file:

The file Sudoku2d.txt contains the following lines:

8, 3, 5, 4, 1, 6, 9, 2, 7
2, 9, 6, 8, 5, 7, 4, 3, 1
4, 1, 7, 2, 9, 3, 6, 5, 8
5, 6, 9, 1, 3, 4, 7, 8, 2
1, 2, 3, 6, 7, 8, 5, 4, 9
7, 4, 8, 5, 2, 9, 1, 6, 3
6, 5, 2, 7, 8, 1, 3, 9, 4
9, 8, 1, 3, 4, 5, 2, 7, 6
3, 7, 4, 9, 6, 2, 8, 1, 5
BUILD SUCCESSFUL (total time: 0 seconds)

If I remove the zero at the top, I get the following errors:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at array.Array.main(Array.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I do not want to have to put a random number at the top of my text files for them to run. Can anyone please tell me why this is happening?

You are getting an error if you remove the first line because only the first line is going into the array (Your 2D array is filled with the single number on your first line). Therefore if you remove the first line, the Integer.parseInt() hits the commas in your text file, and throws an exception.

You need to change your code so that it puts the contents of the text file into the array, one way of doing this is by using your while loop to put the contents of the text file into the array:

String fileName = "Sudoku2d.txt";
Scanner inputStream = null;
String[] line;
System.out.println("The file " + fileName + "\ncontains the following lines:\n");
inputStream = new Scanner(new File("C:\\Users\\username\\Documents\\NetBeansProjects\\Sudoku\\Sudoku2d.txt"));
int[][] puzzel = new int[9][9];
int row = 0;
while (inputStream.hasNextLine()) {
    line = inputStream.nextLine().split(",");
    for (int i = 0; i < 9; i++) {
        puzzel[row][i] = Integer.parseInt(line[i]);
    }
    row++;
}

You can use the following code to check the data that is in your 2D array:

for (int i = 0; i < 9; i++) {
    System.out.println(Arrays.toString(puzzel[i]));
}

If you run this with your current code, you will see the array is filled with only the number on your first line. If you change to my code, it will show you that the data from the text file is going into the array properly (once you remove that single digit on the first line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM