jonathon
jonathon

Reputation: 1

How to check for blank space within a text file

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

Answers (1)

Elliott Frisch
Elliott Frisch

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

Related Questions