Reputation: 1
CURRENT CODE:
String y = getLine();
for (int j = 0; j < 8; j++) {
try {
String[] tokens = y.split("= ");
if (tokens.length > 1 && !tokens[1].isEmpty()) {
dogInfo.add(tokens[1]);
}
y = getLine();
System.out.println(dogInfo.get(i));
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Nothing here!");
} catch(IndexOutOfBoundsException e ) {
System.out.println("Nothing here!");
}
}
I am trying to read a file as follows:
I want add anything after "= " into an arraylist called dogInfo but if there is black space after "= " then I do not want anything added to the arraylist dogInfo
Upvotes: 0
Views: 1125
Reputation: 201447
You add an if
to test if the token is empty. Change this
dogs.add(tokens[1]);
y = getLine();
System.out.println(dogs.get(i));
to something like (you should also test the array length)
if (tokens.length > 1 && !tokens[1].isEmpty()) {
dogs.add(tokens[1]);
System.out.println(tokens[1]);
}
y = getLine();
The !
means "not".
Upvotes: 1