Java中的Scanner类

@version 4.3.2015

@author yNdots 

Java中用到的最普通的从键盘读入数据的代码为Scanner sc=new Scanner(System.in); 这样实例化一个Scanner类之后。这里要提到的是System类作为一个最终类,其具有3个字段,2个为PrintStream类型,即err和out,另外一个就是InputStream类型的in。

但是我遇到一个问题,我从键盘输入一个字符串" ad 132 ",代码如下:

1           Scanner sc=new Scanner(System.in);
2         while(sc.hasNext()){
3 //            System.out.print(sc.nextLine()+" ");/*正确类型*/
4         System.out.print(sc.nextInt()+" ");/*InputMismatchException*/
5 //            System.out.print(sc.next()+" ");/*正确类型*/
6         }
7             sc.close();
我不断的寻找
原文地址:https://www.cnblogs.com/yNds/p/4312957.html