Scanner 类

Java Scanner 类

java.util.Scanner 是 Java5的新特征,咱们能够经过 Scanner 类来获取用户的输入。java

基本语法:code

Scanner sc = new Scanner(System.in);

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

next() 与 nextLine() 区别

next():class

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

nextLine():import

  • 以Enter为结束符,也就是说 nextLine()方法返回的是输入回车以前的全部字符。数据类型

  • 能够得到空白。语法

使用 next 方法:float

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 方法:im

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()方法在后面使用时,注意前面的方法是否含有换行处理。

相关文章
相关标签/搜索