Can somebody please tell me how to parse the following into an int[][]
Type. This is the structure of numbers which are typed into the java args "1,2;0,3 3,4;3,4 "
(the first 4 numbers should represent a matrix as well as the last 4 numbers; so i need both parsed as a int[][]
) but how can i take this and parse it into an int[][]
type ?
First thing would be this i guess :
String[] firstmatrix = args[0].split(";");
String[] rowNumbers = new String[firstmatrix.length];
for(int i=0; i< firstmatrix.length; i++) {
rowNumbers = firstmatrix[i].split(",");
}
but i cant get it to work out -.-
Edit : At first thank you for all your help. But i should have mentioned that exception handling is not necesarry. Also, i am only allowed to use java.lang and java.io
edit 2.0: Thank you all for your help!
public static void main(String[] args) throws IOException {
List<Integer[][]> arrays = new ArrayList<Integer[][]>();
//Considering the k=0 is the show, sum or divide argument
for(int k=1; k< args.length; k++) {
String[] values = args[k].split(";|,");
int x = args[k].split(";").length;
int y = args[k].split(";")[0].split(",").length;
Integer[][] array = new Integer[x][y];
int counter=0;
for (int i=0; i<x; i++) {
for (int j=0; j<y; j++) {
array[i][j] = Integer.parseInt(values[counter]);
counter++;
}
}
//Arrays contains all the 2d array created
arrays.add(array);
}
//Example to Show the result i.e. arg[0] is show
if(args[0].equalsIgnoreCase("show"))
for (Integer[][] integers : arrays) {
for (int i=0; i<integers.length; i++) {
for (int j=0; j<integers[0].length; j++) {
System.out.print(integers[i][j]+" ");
}
System.out.println();
}
System.out.println("******");
}
}
input
show 1,2;3,4 5,6;7,8
output
1 2
3 4
******
5 6
7 8
input for inpt with varible one 3*3 one 2*3 matrix
show 1,23,45;33,5,1;12,33,6 1,4,6;33,77,99
output
1 23 45
33 5 1
12 33 6
******
1 4 6
33 77 99
******