Home  >  Article  >  Java  >  Detailed explanation of java scanner usage

Detailed explanation of java scanner usage

藏色散人
藏色散人Original
2020-05-30 11:39:1810153browse

Detailed explanation of java scanner usage

Detailed explanation of java scanner usage

Java Scanner class

java .util.Scanner is a new feature of Java5. We can obtain user input through the Scanner class.

The following is the basic syntax for creating a Scanner object:

Scanner s = new Scanner(System.in);

Next we demonstrate the simplest data input and obtain the input characters through the next() and nextLine() methods of the Scanner class String, before reading, we generally need to use hasNext and hasNextLine to determine whether there is still input data:

Use next method:

ScannerDemo.java file code:

import java.util.Scanner; 
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
 
        // next方式接收字符串
        System.out.println("next方式接收:");
        // 判断是否还有输入
        if (scan.hasNext()) {
            String str1 = scan.next();
            System.out.println("输入的数据为:" + str1);
        }
        scan.close();
    }
}

The output result of executing the above program is:

$ javac ScannerDemo.java
$ java ScannerDemo

next mode reception:

runoob com

The input data is: runoob

You can see that the com string is not output, next we See nextLine.

Use nextLine method:

ScannerDemo.java file code:

import java.util.Scanner;
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
 
        // nextLine方式接收字符串
        System.out.println("nextLine方式接收:");
        // 判断是否还有输入
        if (scan.hasNextLine()) {
            String str2 = scan.nextLine();
            System.out.println("输入的数据为:" + str2);
        }
        scan.close();
    }
}

The output result of executing the above program is:

$ javac ScannerDemo.java
$ java ScannerDemo

nextLine mode reception:

runoob com

The input data is: runoob com

You can see the com string output.

The difference between next() and nextLine()

next():

1. The input must be completed only after valid characters are read. .

2. The next() method will automatically remove the blanks encountered before entering valid characters.

3. Only after entering valid characters, use the blank space entered after them as the separator or terminator.

next() cannot get a string with spaces.

nextLine():

1. Enter is the end character, which means that the nextLine() method returns all the characters before entering.

2. Can get blank space.

If you want to input int or float type data, it is also supported in the Scanner class, but it is best to use the hasNextXxx() method to verify before inputting, and then use nextXxx() to read:

ScannerDemo.java file code:

import java.util.Scanner;
 
public class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 从键盘接收数据
        int i = 0;
        float f = 0.0f;
        System.out.print("输入整数:");
        if (scan.hasNextInt()) {
            // 判断输入的是否是整数
            i = scan.nextInt();
            // 接收整数
            System.out.println("整数数据:" + i);
        } else {
            // 输入错误的信息
            System.out.println("输入的不是整数!");
        }
        System.out.print("输入小数:");
        if (scan.hasNextFloat()) {
            // 判断输入的是否是小数
            f = scan.nextFloat();
            // 接收小数
            System.out.println("小数数据:" + f);
        } else {
            // 输入错误的信息
            System.out.println("输入的不是小数!");
        }
        scan.close();
    }
}

The output result of executing the above program is:

$ javac ScannerDemo.java
$ java ScannerDemo
输入整数:12
整数数据:12
输入小数:1.2
小数数据:1.2

In the following example, we can enter multiple numbers and find their sum and average. Each time one is entered Use Enter to confirm the number. End the input by entering a non-number and output the execution result:

ScannerDemo.java file code:

import java.util.Scanner;
 
class ScannerDemo {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
 
        double sum = 0;
        int m = 0;
 
        while (scan.hasNextDouble()) {
            double x = scan.nextDouble();
            m = m + 1;
            sum = sum + x;
        }
 
        System.out.println(m + "个数的和为" + sum);
        System.out.println(m + "个数的平均值是" + (sum / m));
        scan.close();
    }
}

The output result of executing the above program is:

$ javac ScannerDemo.java
$ java ScannerDemo
12
23
15
21.4
end
4个数的和为71.4
4个数的平均值是17.85

Recommendation: "javalearning"

The above is the detailed content of Detailed explanation of java scanner usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn