Latest web development tutorials

Java Scanner class

java.util.Scanner Java5 new features, we can get the user's input through Scanner class.

Here is the basic syntax to create a Scanner object:

 Scanner s = new Scanner(System.in); 

Next, we demonstrate a simple data input and the input string available through Scanner class next () and nextLine () method, before reading we generally need to use the data to determine whether there hasNext and haxNextLine entered:

Use 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 above program output is:

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

Com you can see the output string No, let's look at nextLine.

Use nextLine methods:

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 above program output is:

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

Com you can see the output string.

next () and nextLine () difference

next ():

  • 1, be sure to read the end before they can enter a valid character.
  • 2, before entering a valid blank character encountered, next () method will be automatically removed.
  • 3, enter valid only after a blank character behind the input as a delimiter or terminator.
  • next () can not get the string with spaces.

nextLine ():

  • 1, Enter to end the character, that is to say nextLine () method returns all characters before a carriage return.
  • 2 can be obtained blank.

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

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 above program output is:

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

The following examples, we can enter multiple numbers, and for its total and average, each enter a number with, then press Enter to end by entering non-numeric inputs and outputs the results of:

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 above program output is:

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