JavaScanner 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 through the next() and nextLine() methods of the Scanner class String, before reading we generally need Use hasNext and haxNextLine to determine whether there is still input data:
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 output result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo next方式接收: php com 输入的数据为:php
You can see that the com string is not output, let's look at nextLine next.
Use 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 result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo nextLine方式接收: php com 输入的数据为:php com
You can see the com string output.
The difference between next() and nextLine()
next():
1. Be sure to read valid characters before You can end the input.
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 terminator, which means that the nextLine() method returns what was entered before the carriage return. of all characters.
2. You 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:
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 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. Press Enter to confirm each number entered. End the input by entering a non-number and output the execution result:
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 result of executing the above program is:
$ javac ScannerDemo.java $ java ScannerDemo 12 23 15 21.4 end 4个数的和为71.4 4个数的平均值是17.85
For more information, please refer to the API document: http://www.php.cn /manual/jdk1.6/.