Home > Enterprise >  SOLVED - read .csv file into 2D-array
SOLVED - read .csv file into 2D-array

Time:06-08

So i am currently workin on my homework and cant seem to get the .csv file into a 2D-array. The thing is that we have to use the Scanner and the .split() method, no workarounds. currently it seems to only load second to last line into the whole array. The .csv file looks like this:

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

so the problem would be in the while-loop in "readArrayFromFile", everything else has been predetermined.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class array2D{
    private static int[][] readArrayFromFile(String filename) {
        int[][] array = new int[9][9];
        try {
            Scanner myFileReader = new Scanner(new File(filename));

            while (myFileReader.hasNextLine()) {
                String line = myFileReader.nextLine();
                String[] tokens = line.split(";");

                for (int i = 0; i < tokens.length; i  ) {
                    for (int j = 0; j < tokens.length; j  ) {
                        if (myFileReader.hasNext()) {
                            array[i][j] = Integer.parseInt(tokens[j]);
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }
        return array;
    }

    private static void printArray(int[][] inputArray) {
        for (int y = 0; y < inputArray.length; y  ) {
            for (int x = 0; x < inputArray[y].length; x  ) {
                System.out.print(inputArray[y][x]   "\t");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args) {
        String filename = "./src/sudoku1.csv";
        int[][] sudokuField = readArrayFromFile(filename);
        printArray(sudokuField);
    }
}

CodePudding user response:

You don't need 2 loops in while loop, here is the solution.

public class array2D
{
    private static int[][] readArrayFromFile(String filename)
    {
        int[][] array = new int[9][9];
        try
        {
            Scanner myFileReader = new Scanner(new File(filename));

            int i = 0;
            while (myFileReader.hasNextLine())
            {

                String line = myFileReader.nextLine();
                String[] tokens = line.split(";");

                for (int j = 0; j < tokens.length; j  )
                {
                    array[i][j] = Integer.parseInt(tokens[j]);
                }
                i  ;
            }
        }
        catch (FileNotFoundException e)
        {
            System.out.println(e.getMessage());
        }
        return array;
    }

    private static void printArray(int[][] inputArray)
    {
        for (int y = 0; y < inputArray.length; y  )
        {
            for (int x = 0; x < inputArray[y].length; x  )
            {
                System.out.print(inputArray[y][x]   "\t");
            }
            System.out.println();
        }
        System.out.println();
    }

    public static void main(String[] args)
    {
        String filename = "./src/test.csv";
        int[][] sudokuField = readArrayFromFile(filename);
        printArray(sudokuField);
    }
}

CodePudding user response:

From my understanding, your problem is with the nested for loops. Your current code is going to fill the whole 2d array with the last line. My fix would be to place a counter inside the while loop, get rid of one of the for loops, and then it should work. I hope that makes sense

CodePudding user response:

Don't be afraid of using more than one Scanner at once:

private static int[][] readArrayFromFile(String filename) {
    int[][] array = new int[9][9];
    int rowNum = 0;
    try (Scanner myFileReader = new Scanner(new File(filename))) {

        while (myFileReader.hasNextLine()) {
            String line = myFileReader.nextLine();
            Scanner sRow = new Scanner(line);
            sRow.useDelimiter(";");
            int colNum = 0;
            while (sRow.hasNextInt()) {
                array[rowNum][colNum  ] = sRow.nextInt();
            }
            rowNum  ;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return array;
}
  • Related