ylun
ylun

Reputation: 2534

Why do I get a "No line found" Exception in this code?

The following method causes a No line found Exception:

private static String getRebaseAnswer(boolean isFirst, boolean isLast) {
    System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
            + " change this commit's (m)essage?");
    Scanner in = new Scanner(System.in);
    String answer;
    while (true) {
        answer = in.nextLine(); // <--- This Line
        if (answer.equals("c") || answer.equals("m")) {
            in.close();
            return answer;
        } else if (answer.equals("s") && !isFirst && !isLast) {
            in.close();
            return answer;
        } else {
            System.out.println("Would you like to (c)ontinue, (s)kip this commit, or"
                    + " change this commit's (m)essage?");
        }
    }
}

I am calling the method in this method:

...
String answer;
Scanner in;
currHead = branchHeads.get(arg);
while (toRebase != null) {
    System.out.println("Currently replaying:");
    toRebase.getNode().printInfo();
    answer = getRebaseAnswer(isFirst, toRebase.getParent() == null); // <--- This Line
...

What is causing the error?? Shouldn't the scanner wait for me to input a line before continuing the getRebaseAnswer method? A different method in my code has the exact same structure as the above method and encounters no problems. I've checked multiple other posts about this problem but their suggestions are all not pertinent to this problem or do not solve it.

This method runs with no problems:

private static boolean handleDangerous() {
    System.out.println("Warning: The command you entered may alter the files in your"
            + " working directory. Uncommitted changes may be lost. Are you sure you"
            + " want to continue? (yes/no)");
    Scanner in = new Scanner(System.in);
    String answer;
    while (true) {
        answer = in.nextLine();
        if (answer.equals("yes")) {
            in.close();
            return true;
        } else if (answer.equals("no")) {
            in.close();
            return false;
        } else {
            System.out.println("Not a valid answer, please enter (yes/no).");
        }
    }
}

Upvotes: 0

Views: 57

Answers (2)

Prashant
Prashant

Reputation: 2614

don't close scanner otherwise Stream will also be closed.

in.close();

remove this line from current location and put it in main method at the end so after all the operation stream will be closed..

You might be calling some other method which have already closed the stream and then you are calling this method.

Upvotes: 1

merlin2011
merlin2011

Reputation: 75545

When you create a scanner connected to System.in and close it, you also close System.in. Therefore, subsequent attempts to read from System.in will result in the exception you observe.

The way is avoid this is to create the Scanner only once, and never close it until your program is finished. This Scanner should be passed to whichever function needs to read from System.in.

Upvotes: 3

Related Questions