How to exit the program when Ctrl+D is entered in Java?

泄露秘密 提交于 2019-12-20 02:51:02

问题


Below is a section of my Reverse Polish Calculator.

If an integer is entered, push it to the stack and, if = is pressed, peek the result. However, I want to add another condition: if CTRL + D is pressed by the user, the program exits.

I've had a look online but can't seem to find any solutions. Any ideas? Thanks.

Scanner mySc = new Scanner(System.in);
//If input is an integer, push onto stack.
 if (mySc.hasNextInt()) {       
    myStack.push(mySc.nextInt());
} 
//Else if the input is an operator or an undefined input. 
else if (mySc.hasNext()) {
    //Convert input into a string.
    String input = mySc.nextLine(); 
    //Read in the char at the start of the string to operator.
    char operator = input.charAt(0); 
    if (operator == '=') {
        //Display result if the user has entered =.
    }
**else if ("CTRL-D entered") {
    System.exit(0);
    }**

回答1:


Try this:

public static void main(String[] args) {
    try {
        byte[] b = new byte[1024];
        for (int r; (r = System.in.read(b)) != -1;) {
            String buffer = new String(b, 0, r);
            System.out.println("read: " + buffer);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

In this case the loop will stop when you press CTRL+D that is because CTRL+D sends an EOF signal to the System.in InputStream which is -1. That is the case on *nix systems, for Windows system, the EOF signal is sent using the CTRL+Z key combination




回答2:


give this code a try. it actually worked for me

// press ctrl+Z(windows) and ctrl+D(mac, linux) for input termination
StringBuilder out = new StringBuilder();
String text = null;
Scanner scanner = new Scanner( System.in );
while( scanner.hasNextLine() )
{
    text = new String( scanner.nextLine() );
    out.append( text );
}
scanner.close();
System.out.println( out );
System.out.println( "program terminated" );



回答3:


I assume you are talking about a console application?

On unix systems, pressing Ctrl+D closes the stdin for an application. This will result in the input stream underneath Scanner closing, which will cause mySc.hasNext() to return false.

On windows, users need to press Ctrl+Z enter for the same effect.

If the input stream has not been closed, then mySc.hasNext() will block until it can return true.

So you need to wrap your program in a while(mySc.hasNext()) loop, e.g.

    public static void main(String[] args) {

        Scanner mySc = new Scanner(System.in);

        while(mySc.hasNext()) {

//If input is an integer, push onto stack.
            if (mySc.hasNextInt()) {
                System.out.println(String.format("myStack.push(%s);", mySc.nextInt()));
            }
//Else if the input is an operator or an undefined input.
            else {
                //Convert input into a string.
                String input = mySc.nextLine();
                System.out.println(String.format("input = %s", input));

                //Read in the char at the start of the string to operator.
//                char operator = input.charAt(0);
//                if (operator == '=') {
//                    //Display result if the user has entered =.
//                }
//            **else if ("CTRL-D entered") {
//                System.exit(0);
//            }**
            }
        }
    }


来源:https://stackoverflow.com/questions/33940564/how-to-exit-the-program-when-ctrld-is-entered-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!