Java - Scanner用法

2020/09/10

本文介绍下Java中扫描类Scanner的常见用法:

Read Input

首先,是基本的读取:

try (Scanner sc = new Scanner(System.in)){

    System.out.println("Please enter your name: ");
    String name = sc.nextLine();
    System.out.println("name : " + name);

    System.out.println("Please enter your age: ");
    int age = sc.nextInt();
    System.out.println("age: " + age);
}

/** ------------------------*/
// Please enter your name: 
// Yao Frankie
// name : Yao Frankie
// Please enter your age: 
// 24
// age: 24

Split Input

Scanner的默认分隔符是空格,但是你可以通过useDelimiter()进行指定:

String input = "1,2,3,4,5";
try(var sc = new Scanner(input).useDelimiter(",")){
    while (sc.hasNext()){
        System.out.printf(sc.next() + " ");
    }
}

/** ------------------------*/
// 1 2 3 4 5

Read File

然后,你还可以从文件中读取数据:

try (var sc = new Scanner(new File("your_file_path"))){
    while (sc.hasNext()){
        System.out.println(sc.nextLine());
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Input Types

八大基本类型,除了char,其他均有nextXXX()

try(var sc = new Scanner(System.in)){
    System.out.println("Please enter your name, age and salary: ");
    String name   = sc.nextLine();
    int    age    = sc.nextInt();
    double salary = sc.nextDouble();
    System.out.println("Name  : " + name);
    System.out.println("Age   : " + age);
    System.out.println("Salary: " + salary);
}

/** ------------------------*/
// Please enter your name, age and salary: 
// Yao Frankie
// 24 66.20
// Name  : Yao Frankie
// Age   : 24
// Salary: 66.2

next() vs nextLine()

先来看个例子:

try(var sc = new Scanner(System.in)){
    System.out.println("Please enter you name: ");
    String name = sc.next();
    System.out.println("Name: " + name);
}

/** ------------------------*/
// Please enter you name: 
// Yao Frankie
// Name: Yao

我们的期望是返回Yao Frankie,但是只返回了Yao,因为Scanner默认以空格为分隔符。
因此我们可以将分隔符设置为换行符

try(var sc = new Scanner(System.in)){
    System.out.println("Please enter you name: ");
    sc.useDelimiter("\n");
    String name = sc.next();
    System.out.println("Name: " + name);
}

/** ------------------------*/
// Please enter you name: 
// Yao Frankie
// Name: Yao Frankie

也可以直接使用nextLine()

try(var sc = new Scanner(System.in)){
    System.out.println("Please enter you name: ");
    String name = sc.nextLine();
    System.out.println("Name: " + name);
}

/** ------------------------*/
// Please enter you name: 
// Yao Frankie
// Name: Yao Frankie

Reference


一位喜欢提问、尝试的程序员

(转载本站文章请注明作者和出处 姚屹晨-yaoyichen

Post Directory