I'm really new to Java. I'm trying to take values from an input file, which I made in eclipse, and trying to save them to a 2D array. The input is:
31 22 23 79
20 -33 33 1
3 -1 46 -6
I can save it to a regular array fine, but not matter what I try I can't figure out how to get it to save to a 2d array in the form above. I tried for loops, but it saved all 12 numbers for each iteration of the loop. I tried using variables and just incrementing them like for the regular array and it just saved nothing. Any help on how to do this appreciated, code for regular array is below, prints the following to screen:
[31, 22, 23, 79, 20, -33, 33, 1, 3, -1, 46, -6]
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class ArrayMatrix2d {
public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length == 0){
System.err.println("Usage: java Sum <filename>");
System.exit(1);
}
try {
//int[][] matrix = new int[3][4];
int MyMat[] = new int[12];
int num = 0;
//int row = 0;
//int column = 0;
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String line;
while((line = br.readLine()) != null) {
StringTokenizer st = new StringTokenizer (line);
while (st.hasMoreTokens()){
int value1 = Integer.parseInt(st.nextToken());
MyMat[num] = value1;
num++;
}
}
System.out.println(Arrays.toString(MyMat));
br.close();
}
catch(Exception e) {}
}
}