一、Scanner的常用方法

1、next()

从合法字符开始读取,如果正式字符前有空格,读取的时候回忽略;

只读取输入直到空格。它不能读两个由空格或符号隔开的单词。

不能吸收上次输入末尾的回车符(打印时不会显示),所以光标还停留在本行

2、nextLine() 

从第一个字符开始读取,不忽略空格

读取包括单词之间的空格和除回车以外的所有符号(即。它读到行尾)。

可以吸收上次输入末尾的回车符,读取输入后,nextLine()将光标定位在下一行。

3、nextInt()

nextInt() 是取next() 然后把字符串解析成一个int数字。
它本质是调用了next()方法,然后将next()方法返回的字符串再解析成int型数字返回,

所以同样会留下一个“enter” ,光标还停留在本行

nextInt()方法只能接受整型数据,如果输入非整型,就会有异常,就不会读取文本缓冲区的内容,文本缓冲区的内容就不会消失,即使下次再调用nextInt()方法,因为文本缓冲区的内容还是上次的内容,异常继续发生,依次循环。

解决方法:在catch里加上个input.next();  或者input.nextLine();  即可

因为next()方法可以接受字符串,调用此方法后,就会读取文本缓冲区的内容并以字符串返回,相当于把文本缓冲区清空了。

Scanner只要 读取成功,缓冲区就自动清空

4、.hasNextInt()

因为在 input.hasNextInt() 仅仅只是判断标记的是否为 int 类型的数据 ,并不会将标记移到下一行,所以其若输入非 int 类型的数据会无限输出 "请输入数字" ,而不会做下一个判断。

hasNextInt() 是判断下次调用next()时否可以得到一个可以安全解析成int的字符串。下一个next()的返回值可以解析为一个数字,即符合数字的格式,那么返回true。注意当下一个字符串不能被解析成数字是,这个方法并不会返回false。
 

 

二、报错案例

1、循环输入数字

问题:让用户不断输入数字,但是在运行过程中,一旦输入不合法,就会无限循环

package cn.itcast.scannerLoopNumber;

import java.util.Scanner;

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/4 9:13
 * @describe: 测试Scanner循环输入数字
 */
public class LoopInputNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        //如果输入不合法,会进行无限循环
        while (true) {
            System.out.print("请输入数字:");
            if (sc.hasNextInt()) {
                int a = sc.nextInt();
                System.out.println("a = " + a);
            } else {
                System.out.println("\r\n这不是数字");
            }

        }

    }
}


输出
输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:
这不是数字
请输入数字:

原因:如果输入非整型,就会有异常,就不会读取文本缓冲区的内容,文本缓冲区的内容就不会消失,即使下次再调用nextInt()方法,因为文本缓冲区的内容还是上次的内容,异常继续发生,依次循环。

解决办法:

在catch里加上个sc.next();  或者sc.nextLine() 即可

因为next()方法可以接受字符串,调用此方法后,就会读取文本缓冲区的内容并以字符串返回,相当于把文本缓冲区清空了。

Scanner只要 读取成功,缓冲区就自动清空

package cn.itcast.scannerLoopNumber;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/4 9:13
 * @describe: 测试Scanner循环输入数字
 */
public class LoopInputNumber {
    public static void main(String[] args) {
        int number=0;
        Scanner input=new Scanner(System.in);
        while(true){
            if(input.hasNextInt()){
                System.out.println("判断之后");
                number=input.nextInt();
               System.out.println("number = "+ number);
            }else{
                String s = input.nextLine();
                //String s = input.next();
                System.out.println("输入不合法,当前行内容是 "+s);
                System.out.println("请输入数字");
            }
        }
     
    }
}


输出
1
判断之后
number = 1
2
判断之后
number = 2
3
判断之后
number = 3
q
输入不合法,当前行内容是 
请输入数字
输入不合法,当前行内容是 q
请输入数字
q
输入不合法,当前行内容是 q
请输入数字

问题描述:无限循环问题解决了,但是出现另外一个问题,使用nextLine(),第一次错误输入时,会报错两次

原因分析:nextInt()是以next()为基础,不吸收末尾的“enter”,

                  最后一次调用nextInt,只取走了数字部分,还留下“enter”

                  调用nextLine()时,会先读取上次的“enter”,

解决方法:换成next()

package cn.itcast.scannerLoopNumber;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/4 9:13
 * @describe: 测试Scanner循环输入数字
 */
public class LoopInputNumber {
    public static void main(String[] args) {
        int number=0;
        Scanner input=new Scanner(System.in);
        while(true){
            if(input.hasNextInt()){
                System.out.println("判断之后");
                number=input.nextInt();
               System.out.println("number = "+ number);
            }else{
//                String s = input.nextLine();
                String s = input.next();
                System.out.println("输入不合法,当前行内容是 "+s);
                System.out.println("请输入数字");
            }
        }
     
    }
}


输出
1
判断之后
number = 1
2
判断之后
number = 2
3
判断之后
number = 3
q
输入不合法,当前行内容是 q
请输入数字
e
输入不合法,当前行内容是 e
请输入数字

2、先输入数字 ,再输入字符

字符自动填充了

package cn.scanner;

import java.util.Scanner;

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/4 10:23
 * @describe:
 */
public class TestScanner {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        // 先读取键盘输入的int值
        System.out.println("input id :");
        int id = sc.nextInt();
        // 后读取键盘输入的字符串
        System.out.println("input name :");
        String name = sc.nextLine();
        System.out.println("id = " + id + " name =[" + name + "]");
        System.out.println("execute finish !");
        
    }
}

java 控制台连续输入 java键盘连续输入_字符串

原因分析:nextInt() 是取next() 然后把字符串解析成一个int数字。
                 它本质是调用了next()方法,然后将next()方法返回的字符串再解析成int型数字返回。

                 键盘输入的实际是 "0"+"Enter",nextInt()只用了“0”,留下“enter” , 调用nextLine()的时候,先读取了“enter”

解决办法:用next() 替换nextLine()

package cn.scanner;

import java.util.Scanner;

/**
 * @author wangyue
 * @version 1.0
 * @date 2019/7/4 10:23
 * @describe:
 */
public class TestScanner {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        // 先读取键盘输入的int值
        System.out.println("input id :");
        int id = sc.nextInt();
        // 后读取键盘输入的字符串
        System.out.println("input name :");
        //String name = sc.nextLine();
        String name = sc.next();
        System.out.println("id = " + id + " name =[" + name + "]");
        System.out.println("execute finish !");

    }
}

java 控制台连续输入 java键盘连续输入_字符串_02

三、注意事项

1、数字和字符交替输入的时候,尽量先用nextLine,再用nextInt; 或者用过nextInt,跟着一个nextLine吸收掉”enter“