Java Scanner Class

Source: Internet
Author: User

Java Scanner Class

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

Here is the basic syntax for creating a Scanner object:

New Scanner (system.in);

Next we demonstrate the simplest data input, and get the input string through the next () and Nextline () method of the Scanner class, before reading we generally need to use Hasnext and hasnextline to determine if there is any data to be entered:

Use the next method: Scannerdemo.java file code:
ImportJava.util.Scanner;  Public classScannerdemo { Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); //receiving data from the keyboard//next way to receive stringsSYSTEM.OUT.PRINTLN ("Next mode receives:"); //determine if there is any input        if(Scan.hasnext ()) {String str1=Scan.next (); System.out.println ("The input data is:" +str1); }       }  }

Execute the above program output as:

$ javac scannerdemo.java$ java scannerdemonext mode receive: Runoob COM input data is: Runoob

You can see the COM string is not output, next we look at nextline.

Use the Nextline method: Scannerdemo.java File code:
ImportJava.util.Scanner;  Public classScannerdemo { Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); //receiving data from the keyboard//nextline Way to receive stringsSystem.out.println ("Nextline Way to receive:"); //determine if there is any input        if(Scan.hasnextline ()) {String str2=Scan.nextline (); System.out.println ("The input data is:" +str2); }       }  }

Execute the above program output as:

$ javac scannerdemo.java$ java scannerdemonextline mode receive: Runoob COM input data is: Runoob com

You can see the COM string output.

Next () differs from nextline ()

Next ():

    • 1. Be sure to read the valid characters before you can end the input.
    • 2. The next () method will automatically remove the whitespace that is encountered before entering a valid character.
    • 3. Enter only the valid characters before entering the blanks as delimiters or terminators.
    • Next () cannot get a string with a space.

Nextline ():

    • 1. Enter as the Terminator, meaning that the nextline () method returns all characters before the input carriage return.
    • 2, you can get blank.

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

Scannerdemo.java File Code:
Vimport Java.util.Scanner;  Public classScannerdemo { Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); //receiving data from the keyboard        inti = 0 ; floatf = 0.0f ; System.out.print ("Enter integer:"); if(Scan.hasnextint ()) {//determines whether the input is an integeri =Scan.nextint (); //receive integersSystem.out.println ("Integer data:" +i); }Else{                                       //Enter the wrong informationSYSTEM.OUT.PRINTLN ("Input is not an integer! ") ; } System.out.print ("Enter decimal:"); if(Scan.hasnextfloat ()) {//determine if the input is a decimalf =scan.nextfloat (); //Receive decimalsSYSTEM.OUT.PRINTLN ("Decimal Data:" +f); }Else{                                      //Enter the wrong informationSYSTEM.OUT.PRINTLN ("The input is not a decimal!") ") ; }      }  }

Execute the above program output as:

$ javac scannerdemo.java$ java scannerdemo input integer: integer data: input decimal number:1.2 Decimal data:1.2

The following example we can enter a number of numbers, and the sum and average, each input a number with a return confirmation, by entering a non-numeric to end the input and output the execution result:

Scannerdemo.java File Code:
ImportJava.util.Scanner; classScannerdemo { Public Static voidMain (string[] args) {Scanner scan=NewScanner (system.in); Doublesum = 0; intm = 0;  while(Scan.hasnextdouble ()) {Doublex =scan.nextdouble (); M= m + 1; Sum= Sum +x; } System.out.println (M+ "Number of" and "+"sum); System.out.println (M+ "The average of the number is" + (sum/m)); }  }

Execute the above program output as:

$ javac scannerdemo.java$ java scannerdemo12231521.4end4 number of and is. 4The average of 4 numbers is. 85

Java Scanner Class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.