Java Scanner class

java.util.Scanner is a new feature of Java 5. We can use the Scanner class to get user input.
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 use the next() and nextLine() methods of the Scanner class to get the input string. Before reading, we generally need to use hasNext and hasNextLine to determine if there is any input data:

Use the 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); // Receive data from the keyboard //next way to receive a string System.out.println("next way to receive:"); // Judge whether there is input if (scan.hasNext()) { String str1 = scan.next(); System.out.println("The input data is: " + str1); } scan.close(); } }
The output of the above program is:
$ javac ScannerDemo . java
$ java ScannerDemo next way to receive:

interviewbubble com
The data entered is: interviewbubble
You can see that the com string is not output. Next we see nextLine.

Use the 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); // Receive data from the keyboard // nextLine method to receive a string System.out.println("NextLine method receives:"); // Judge whether there is input if (scan.hasNextLine()) { String str2 = scan.nextLine(); System.out.println("The input data is:" + str2); } scan.close(); } }
The output of the above program is:
$ javac ScannerDemo . java
The $ java ScannerDemo 
nextLine method receives:
bubble com
The data entered is: bubble com
You can see the com string output.

The difference between next() and nextLine()

Next():
  • 1. Be sure to read the valid characters before ending the input.
  • 2. The white space encountered before the input of a valid character is automatically removed by the next() method.
  • 3, only enter the valid characters after the blank input as a delimiter or terminator.
  • Next() cannot get a string with spaces.
nextLine():
  • 1, with Enter as the terminator, which means that the nextLine () method returns all the characters before the input carriage return.
  • 2, can get blank.
If you want to enter data of type int or float, there is also support in the Scanner class, but it is best to use the hasNextXxx() method for validation before input, 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);
// Receive data from the keyboard int i = 0 ; float f = 0 .0 f ; System . out . print ( " Input Integer : " ) ;
if ( scan . hasNextInt ( ) ) {
// judge if the input is an integer i = scan . nextInt ( ) ;
// receive integer System . out . println ( " Integer data: " + i ) ; } else {
// Enter the wrong information System . out . println ( " input is not an integer! " ) ; }
System . out . print ( " input decimal: " ) ; if ( scan . hasNextFloat ( ) ) { // determine if the input is a decimal f = scan . nextFloat ( ) ; // receive decimals System . out . println ( " Decimal data: " + f ) ; } else {
// Enter the wrong information System . out . println ( " Input is not a decimal! " ) ; }
scan . close ( ) ; }
}
The output of the above program is:
$ javac ScannerDemo.java
$ java ScannerDemo
Input Integer::12
Integer Data:12
Input Decimals:1.2
Decimal Data:1.2
In the following example, we can input multiple numbers and find the sum and average. Enter a number for each entry and confirm with Enter. End the entry by entering non-digits and output the 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(" The sum is "+ sum); System.out.println( "The average of the "+ m + "number is" + (sum / m ));
scan . close ( ) ; }
}
The output of the above program is:
$ javac ScannerDemo.java
$ java ScannerDemo
12
23
15
21.4
end
 The sum is 71.4
The average of the "+ m + "number is17.85

Comments