I am trying to read elements of a 2-d 3x3 matrix using a scanner. The input would look something like this:
3
11 2 4
4 5 6
10 8 -12
I am current geting the error:
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
scan.next();
List<List<Integer>> array = new ArrayList<>();
for (int i = 0; i < a; i++) {
String number = scan.nextLine();
String[] arrRowItems1 = number.split(" ");
List<Integer> list = new ArrayList<>();
for (int j = 0; j < a; j++) {
int arrItem = Integer.parseInt(arrRowItems1[j]);
list.add(arrItem);
}
array.add(list);
}
scan.close();
How do I go about doing this problem, so that a the end a 2d 3x3 matrix array is constructed according to user input? Thank You.
11 2 4or should this be separate inputs11,2and4?