Java Scanner class

Java.util.Scanner is a new feature of Java 5, and we can get user input through the Scanner class.

Here's the basic syntax for creating scanner objects:

 Scanner s = new Scanner(System.in); 

Next we demonstrate one of the simplest data inputs and get the input strings through the scanner class's next() and nextLine() methods, and we generally need to use hasNext and hasNextLine to determine if there is any data left before reading:

Using the next method:

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);  
        }  

    }  
} 

The output of the above program is:

$ javac ScannerDemo.java
$ java ScannerDemo
next方式接收:
youj com
输入的数据为:youj

You can see that the com string is not output, so let's look at nextLine.

Using the nextLine method:

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);  
        }  

    }  
} 

The output of the above program is:

$ javac ScannerDemo.java
$ java ScannerDemo
nextLine方式接收:
youj com
输入的数据为:youj com

You can see the com string output.

Next() is different from nextLine().

next():

  • 1, must read to the valid character before you can end the input.
  • 2, the next() method will automatically remove the blanks encountered before entering valid characters.
  • 3, only after entering a valid character to enter the blank as a separator or end character.
  • Next() cannot get a string with spaces.

nextLine():

  • 1, with Enter as the end character, that is, the nextLine() method returns all the characters before entering the carriage return.
  • 2, you can get blank.

If you want to enter data of the int or float type, it is also supported in the Scanner class, but it is best to use the hasNextXxx() method for validation before entering, and then read using nextXxx() before entering:

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("输入的不是小数!") ;  
        }  
    }  
} 

The output of 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 sum them with the average, confirm each number with carriage return, end the input by entering non-numbers, and output the execution results:

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));  
    }  
}  

The output of the above program is:

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

More information can be referred to the API documentation: //www.w3cschool.cn/manual/jdk1.6/.