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

后端 未结 3 856
半阙折子戏
半阙折子戏 2021-01-22 18:21

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 a

3条回答
  •  迷失自我
    2021-01-22 18:53

    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" );
    

提交回复
热议问题