Scanner 类

# Java Scanner 类

java.util.Scanner 是 Java5的新特征,我们可以通过 Scanner 类来获取用户的输入。

基本语法:

Scanner sc = new Scanner(System.in);

通过Scanner类的next()或nextLine()方法获取输入的字符串, 在读取前我们一般需要 使用 hasNext 与 hasNextLine 判断是否还有输入的数据:

next() 与 nextLine() 区别

next():

  • 一定要读取到有效字符后才可以结束输入。
  • 对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
  • 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
  • next() 不能得到带有空格的字符串。

nextLine():

  • 以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。

  • 可以获得空白。

使用 next 方法:

import java.util.Scanner;

public class ScannerDemo {
    public static void main(String[] args) {
        //首先创建一个扫描器,用于接收键盘数据
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数据:");
        if (sc.hasNext()) {
            String str = sc.next();
            System.out.println("收到的数据为:" + str);
        }
        sc.close();
    }
}

结果

请输入数据:
hello world
收到的数据为:hello

使用 next 方法:

import java.util.Scanner;

public class ScannerNextLine {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入数据:");
        if (sc.hasNextLine()) {
            String str = sc.nextLine();
            System.out.println("收到的数据为:" + str);
        }
        sc.close();
    }
}

结果

请输入数据:
hello world
收到的数据为:hello world

输入基本数据类型

如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,

但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取:

比如输入int类型的数据

if (sc.hasNextInt()) {
    int i = sc.nextInt();
    System.out.println("收到的数据为:" + i);
}

练习

输入多个数字,并求其总和与平均数,通过输入非数字来结束输入并输出执行结果:

import java.util.Scanner;

public class Sum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        int count = 0;
        while (sc.hasNextInt()) {
            int num = sc.nextInt();
            sum += num;
            count++;
        }
        System.out.println("总和:" + sum);
        System.out.println("平均:" + (1.0 * sum / count));

        sc.close();
    }
}

结果·

1 2 3 4 5 q
总和:15
平均:3.0

注意

当使用nextLine()方法之前,使用过其他的方法,需要多使用一次nextLine()方法

如下代码,先输入一个年龄,再输入姓名

import java.util.Scanner;

public class ScannerBug {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = 0;
        String str = null;
        if (sc.hasNextInt()) {
            num = sc.nextInt();
        }
        if (sc.hasNextLine()) {
            str = sc.nextLine();
        }
        System.out.println("---------输出结果-------");
        System.out.println("年龄" + num);
        System.out.println("姓名" + str);

        sc.close();
    }
}

结果

18
---------输出结果-------
年龄18
姓名

分析

在输入18按下回车之后,直接打印结果。跳过了姓名的输入,不妨试试使用空格分离输入的参数。

18 ljh
---------输出结果-------
年龄18
姓名 ljh

发现姓名后面多了个空格才打印输入的名字,说明nextInt()方法只读取到18。

18往后的数据都被nextLine()接收,正好nextLine()方法可以接收空白,所以包含了空格。

如果将nextLine()方法改成next()方法,不包含空白,可以解决以上问题。

package com.study.scanner;

import java.util.Scanner;

public class ScannerBug {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int num = 0;
        String str = null;
        if (sc.hasNextInt()) {
            num = sc.nextInt();
        }

        if (sc.hasNext()) {
            str = sc.next();
        }

        System.out.println("---------输出结果-------");
        System.out.println("年龄" + num);
        System.out.println("姓名" + str);

        sc.close();
    }
}

结果

18 ljh
---------输出结果-------
年龄18
姓名ljh


18
ljh
---------输出结果-------
年龄18
姓名ljh

那么就是要求nextLine()读取下一行数据怎么办呢?

如新增一个需求再读取一个电话号码,要求区号和尾号用空格隔开

public class ScannerBug {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int num = 0;
        String str = null;
        String phone = null;
        if (sc.hasNextInt()) {
            num = sc.nextInt();
        }
        if (sc.hasNext()) {
            str = sc.next();
        }
        if (sc.hasNextLine()) {
            phone = sc.nextLine();
        }
        System.out.println("---------输出结果-------");
        System.out.println("年龄" + num);
        System.out.println("姓名" + str);
        System.out.println("电话" + phone);

        sc.close();
    }
}

结果

18
ljh
---------输出结果-------
年龄18
姓名ljh
电话


18 ljh 0771 1234
---------输出结果-------
年龄18
姓名ljh
电话 0771 1234

两种输入方式,第一种 nextLine()方法又读取了ljh后面的数据,导致第三个数据不能输入。

第二种输入方式多了个空格

考虑应该把ljh后面的数据给吃掉。再接收第三个数据

import java.util.Scanner;

public class ScannerBug {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int num = 0;
        String str = null;
        String phone = null;
        if (sc.hasNextInt()) {
            num = sc.nextInt();
        }

        if (sc.hasNext()) {
            str = sc.next();
        }

        if (sc.hasNextLine()) {
            sc.nextLine();// 去除前一行多余的数据
            phone = sc.nextLine();
        }

        System.out.println("---------输出结果-------");
        System.out.println("年龄" + num);
        System.out.println("姓名" + str);
        System.out.println("电话" + phone);

        sc.close();
    }
}

结果

18
ljh
0771 1234
---------输出结果-------
年龄18
姓名ljh
电话0771 1234

最终达到想要的效果。

总结

nextXxx()方法只读取到相应的数值就停止,不含换行操作。

nextLine()方法在后面使用时,注意前面的方法是否含有换行处理。

posted @ 2020-05-06 16:06  小橘子ღ  阅读(287)  评论(0编辑  收藏  举报