Reputation: 73
Am trying to create an array which holds the String arrays created from a utilising the String.split()
method on a string which is input through a loop.
int N = in.nextInt();
String [][] defibDetails = new String[N][];
in.nextLine();
for (int i = 0; i < N; i++) {
String DEFIB = in.nextLine();
String [] details = DEFIB.split(";");
defibDetails[i][]=details;
}
System.out.println(defibDetails[0][0]);
I would like the System.out to be the substring of DEFIB
before the first ;
where loop counter i = 0
. Thanks for any thoughts.
Upvotes: 0
Views: 78
Reputation: 2704
The error is here: defibDetails[i][]
; it should be like this: defibDetails[i]
You've got a two dimensional array, so fist dimension is particular array of "array of arrays". Second is particular element of this array.
So defibDetails[i]
means array #i, defibDetails[i][j]
means element j in array i.
int N = in.nextInt();
String [][] defibDetails = new String[N][];
in.nextLine();
for (int i = 0; i < N; i++) {
String DEFIB = in.nextLine();
String [] details = DEFIB.split(";");
defibDetails[i]=details; // <<<<<<<<
}
System.out.println(defibDetails[0][0]);
Upvotes: 1