java处理Ctrl z强行关闭输入流

控制台程序输入Ctrl z 标志着输入的结束,和C中的EOF类似。
1)Scanner.next() -->NoSuchElementException
2)Scanner.hasNext()–>false
平时用的都是hasNext(),所以这里只处理第二种情况。
其实也很简单,就是输出一个提示信息,然后结束程序就是了:

//平时都是只有这个if条件,没有后面那个else语句。这里就加个else语句处理一下异常情况即可
if(scan.hasNextLine())
     s=scan.nextLine();
else{
     System.out.println("you end the input stream!");
     return;
    }

猜你喜欢

转载自blog.csdn.net/yuanren201/article/details/88765915