简体   繁体   中英

Strange error with Java Scanner when taking user input

I am working on a Java project that includes a simple menu system. I am getting some strange errors when the code reaches folling line in the Menu() function:

int menuInput = Integer.parseInt(scanner.nextLine()); 

I have seen similar issues that people have posted, but nothing I do seems to fix the issue. I have posted the full class below.

package Compiler;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        System.out.println("Copy and paste code to be compiled here, then press ENTER followed by CTRL+D:\n");

        CharStream input = CharStreams.fromStream(System.in);
        GrammarLexer lexer = new GrammarLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        GrammarParser parser = new GrammarParser(tokens);
        ParseTree tree = parser.program();
        Worker worker = new Worker(parser.getRuleNames());
        worker.visit(tree);
        Menu(worker);
    }

    private static void Menu(Worker worker) {
        while (true) {
            System.out.println("1-4?");

            Scanner scanner = new Scanner(System.in);
            int menuInput = Integer.parseInt(scanner.nextLine());   // THIS IS WHERE THE CODE CRASHES

            try {
                switch(menuInput) {
                    case 1: {
                        worker.PrintParseTree();
                        break;
                    } case 2: {
                        String searchQuery = scanner.nextLine();
                        worker.SearchParseTree(searchQuery);
                        break;
                    } case 3: {
                        worker.PrintJSCode();
                        break;
                    } case 4: {
                        worker.PrintWACode();
                        break;
                    } default: {
                        System.out.println("ERROR: Invalid input, please try again...");
                        break;
                    }
                }
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
}

This is the error that continues to occur. The code crashes before giving the user opportunity to make any input:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
    at Compiler.Main.Menu(Main.java:44)
    at Compiler.Main.main(Main.java:29)

Update: From looks of things from ANTLR website, the fromStream method will open the System.in stream, then closes it (see quote from website).

Creates a CharStream given an opened InputStream containing UTF-8 bytes. Reads the entire contents of the InputStream into the result before returning, then closes the InputStream.

Can anybody provide a workaround for this?

I think somewhere inside of the worker methods you close the CharStream input, which closes System.in, which means there are no more lines for the scanner to read.

Edit:

I don't have access to the CharStream class, so I substited it for InputStreamReader in my example. The following code gives the same error that yours does.

//When the program starts, System.in is open.

InputStreamReader is = new InputStreamReader(System.in);
is.close(); 
// When we close the InputStreamReader, the underlying System.in is also closed.

Scanner scanner = new Scanner(System.in); 
//It successfully creates a scanner for System.in, even if it's closed.

scanner.nextLine();
//But it can't read, since the underlying stream is now closed.

I'm not concerned about instances of System.in.close() , but rather input.close() which also affects System.in . input may of course have a different name in different classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM