GregB
GregB

Reputation: 73

Adding string arrays to a 2d String array through iteration

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

Answers (1)

B-GangsteR
B-GangsteR

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]);

http://ideone.com/Dr9Aci

Upvotes: 1

Related Questions