Home > Back-end >  Java String array values to 2D int array
Java String array values to 2D int array

Time:04-14

I am a beginner and I have a matrix in a CSV file like this:

0;1;1;0
1;0;1;1
1;1;0;0
0;1;0;0

And I want to import it into a 2D int array so I can do multiplications with it and calculate some stuff for my graph program. This is my code so far:

try
    {
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line = "";

        while ((line = br.readLine()) != null)
        {
            String[] values = line.replaceAll("\\D ","").split(";");

            for (int i = 0; i < values.length; i  )
            {
                for (int j = 0;j< values.length;j  )
                {
                    test[i][j] = Integer.parseInt(values[i]);
                    System.out.print(test[i][j]);
                }
                System.out.println();
            }
        }
        System.out.println("****");
    } catch (IOException e)
    {
        e.printStackTrace();
    }

My problem is that it will put all values from the first line into the first [] of the matrix and doesn't put every single value in each []. I tried different approaches but I always have the same outcome. I kindly ask for help.

Edit: I didn't clarify my issue properly. My code takes the first like "0110" and doesn't put each value into each place of the first line of my matrix. It should be test[0][0] has the value 0, test[0][1] has the value 1, test[0][2] has the value 1, test[0][3] has the value 0. And the same for the other lines. But it puts the whole line in [0][0] and also cuts off the first number. So it doesn't have 0110 in that place but 110.

CodePudding user response:

You need to have an increment variable that can be used to change the row for the matrix. Try the below code:

try {
BufferedReader br = new BufferedReader(new FileReader(path));
String line = "";
int rowNumber = 0;
while ((line = br.readLine()) != null) {
    String[] values = line.split(";");
    for (int j = 0; j < values.length; j  ) {
        test[rowNumber][j] = Integer.parseInt(values[j]);
        System.out.print(test[rowNumber][j]);
    }
    rowNumber  ;
    System.out.println();
}
System.out.println("****");
} catch (IOException e) {
    e.printStackTrace();
}

CodePudding user response:

Use the while loop to deal with each line. Use a variable (i below) to remember the line you're in. Then just loop over the splitted line like you alread do, but use the correct index for values (j, not i):

    BufferedReader br = new BufferedReader(new FileReader(path));
    String line = "";

    int i = 0; //to remember the line
    while ((line = br.readLine()) != null) {
        String[] values = line.replaceAll("\\D ","").split(";");
        for (int j = 0; j<values.length; j  ) {
            test[i][j] = Integer.parseInt(values[j]); //use j here, not i!
            System.out.print(test[i][j]);
        }
        i  ;
    }

CodePudding user response:

Try using just "i" as index for values, as the inner loop is not needed, you read values from csv in line-by-line manner, do something like this:

try {
    BufferedReader br = new BufferedReader(new FileReader(path));
    String line = "";
    
    int i = -1;    //here i will act as number of lines that have been "read"
    while ((line = br.readLine()) != null) {
        i =1;    //incrementing i to increase line count, and move array to next iteration
        String[] values = line.replaceAll("\\D ","").split(";");
        for (int j = 0; j < values.length; j  ) {
            test[i][j] = Integer.parseInt(values[j]);
            System.out.print(test[i][j]);
            System.out.println();
        }    //j for loop close
    }    //while close
    System.out.println("****");
} catch (IOException e) {
    e.printStackTrace();
}
  • Related