zulululu
zulululu

Reputation: 5

Java: Reading lines from a file when not all lines have a new line

I'm working on a program that opens a data file and reads the file line by line using a while loop to store data in a binary search tree. My only problem though is I'm not sure how to handle the loop condition, as the last line of the file does not have a new line character. I usually use hasNextLine() with a scanner for reading through a file, but this throws errors in my program, and I can't use hasNext() because I need to grab the entire line and not just pieces of it. What would be the best way to go about this?

    public static BinaryTree getInventory(BinaryTree tree, Scanner input) {
    String line, title = "";
    int available = -1, rented = -1, comma = 0;
    boolean quote = true;

    // While not the end of file
    while (input.hasNextLine()) {
        line = input.nextLine();

        for (int i = 1; i < line.length(); i++) {
            if (line.charAt(i) == '"') {
                title = line.substring(1, i);
                comma = i + 1;
                quote = false;
                i++;
            } 
            else if (line.charAt(i) == ',' && !quote) {
                available = Integer.parseInt(line.substring(comma + 1, i - 1));
                comma = i;
                i++;
            } 
            else if (i + 1 == line.length()) {
                rented = Integer.parseInt(line.substring(comma + 1, i));
            }

        }
        tree.insert(new Node(title,available,rented));
    }
    return tree;
}

Upvotes: 0

Views: 272

Answers (2)

phatfingers
phatfingers

Reputation: 10250

The accepted answer should solve your problem just fine, but I think it's worth pointing something out so you (or people learning from your post) correctly understand how Scanner works.

Given an input source like Line 1\nLine 2, where Line 2 does not end in a newline character, the following code will print both lines without throwing any errors.

while (input.hasNextLine()) {
    System.out.println(input.nextLine());
}

This will also print both lines.

while (input.hasNext()) {
    System.out.println(input.nextLine());
}

Given your example code, I think it's most likely that one of your titles contained a comma, which would lead to Integer.parseInt trying to parse some text that isn't an integer. When asking for help troubleshooting code that throws an error, it's helpful to share the exception and possibly a stack trace.

Upvotes: 0

Serg M Ten
Serg M Ten

Reputation: 5606

You can avoid the need of hasNext() by catching NoSuchElementException. Also given the structure of your file (which seems to be "title",XX,YY) you an avoid looping over line characters with a code like this

try {
    while (true) {
        String line = input.nextLine();
        int rdquo = line.indexOf('"', 1);
        String title = line.substring(1, rdquo);
        String[] avail = line.substring(rdquo+2).split(",");
        tree.insert(new Node(title, Integer.parseInt(avail[0]) , Integer.parseInt(avail[1]));
    }
} catch (NoSuchElementException reachedEndOfFile) { }

Upvotes: 1

Related Questions