0

I have a text file that I have to store into a 2d array, with 3 columns, and numRecords rows, but is implemented within a method. The parameter numRecords is the number of values to read from the input file. I'm getting an InputMismatchException and I can't figure out why. Any help would be greatly appreciated

public String[][] readFile(File file, int numRecords) throws IOException {

    int numRows = numRecords;
    int numColumns = 3; // column 1 will be age, column 2 will be height, column 3 will be weight
    String[][] data = new String[numRows][numColumns]; 

    try {
        Scanner readFile = new Scanner(file);
        String line = null;
        line = readFile.nextLine().trim();

        while (readFile.hasNextLine()) {
            line = readFile.nextLine();
            String[] str = line.split(",");

            for (String element : str) {            
                element = element + " "; 

                for (int row = 0; row < data.length; row++) {
                    for (int column = 0; column < data[0].length; column++) {

                        data[row][column] = Integer.toString(readFile.nextInt());
                        data[row][column] = Integer.toString(readFile.nextInt());
                        data[row][column] = Integer.toString(readFile.nextInt());
                    }
                } 
            }
        }

        readFile.close();
    }

    catch (InputMismatchException e1) {
        e1.printStackTrace();
    }

    return data;
}
Alan Sereb
  • 1,657
  • 1
  • 10
  • 26
  • 1
    What is your file like? Can you give an example of your file? – Sweeper Aug 31 '19 at 01:25
  • What is the purpose of the outer while loop if you are attempting to fill the entire array in the double for loop during the first iteration of the outer while loop? Also, I would advise against using nextInt() [as this can lead to problems if it encounters newline characters](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo). – Mihir Kekkar Aug 31 '19 at 01:28

1 Answers1

0

You are generating element(s) from your scanner by reading line(s). You are also attempting to then read int(s) from the scanner and ignoring the elements you have read. Another possible issue is your code doesn't test that the file can actually be read before starting. And I would prefer try-with-Resources over explicitly closing the Scanner (as here you leak a file handle whenever there is an exception). Also, why read int(s) and convert them to a String - I would prefer reading into an int[][]. Putting that all together,

public int[][] readFile(File file, int numRows) throws IOException {
    if (!file.exists() || !file.canRead()) {
        System.err.printf("Cannot find file: %s%n", file.getCanonicalPath());
        return null;
    }
    int numColumns = 3; 
    int[][] data = new int[numRows][numColumns];

    try (Scanner readFile = new Scanner(file)) {
        while (readFile.hasNextLine()) {
            String line = readFile.nextLine();
            String[] tokens = line.split(",");
            int row = 0, column = 0;
            for (String element : tokens) {
                if (column >= numColumns) {
                    column = 0;
                    row++;
                }
                data[row][column] = Integer.parseInt(element);
                column++;
            }
        }
    } catch (InputMismatchException e1) {
        e1.printStackTrace();
    }

    return data;
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226