Java Api--scanner Class

Source: Internet
Author: User

1, Scanner class overview

JDK5 is used later to get the user's keyboard input, a simple text scanner that can use regular expressions to parse basic types and strings. Scanner uses the delimiter mode to break its input into markup, which by default matches whitespace. You can then use a different next method to convert the resulting token to a different type of value. 2, now use the construction method public Scanner (InputStream source) 3, Scanner class member Method 1) Basic Format hasnextxxx () Determine if there is a next entry, where xxx can be Int,dou ble and so on.    If you need to decide whether to include the next string, you can omit xxx. Nextxxx () Gets the next entry.  XXX has the same meaning as in the previous method. 2) By default, scanner uses spaces, carriage returns, etc. as separators3) Example: an example of a method with int type
Public Boolean hasnextint ()public int Nextint ()
 Public classScannerdemo { Public Static voidMain (string[] args) {//Creating ObjectsScanner sc =NewScanner (system.in); //gets the data, first to determine whether the input data matches the type of the received variable        if(Sc.hasnextint ()) {intx =Sc.nextint (); System.out.println ("x:" +x); } Else{System.out.println ("The data you entered is wrong"); }    }}
4) Analyze the combination of the following two methods using: public int nextint () public string nextline () The problem occurs if you first get a numeric value and then get a string. The main reason: that is the problem of the newline symbol.    Problem solving: A: Gets a string after a value is first obtained and a new keyboard entry object is created. B: Put all the data in the string first, then what you want, and what you convert to.
 Public classScannerdemo { Public Static voidMain (string[] args) {//Creating ObjectsScanner sc =NewScanner (system.in); //get a value of two int type        intA1 =Sc.nextint (); intB1 =Sc.nextint (); System.out.println ("A:" + A1 + ", B:" +B1); System.out.println ("-------------------"); //gets a value of two string typeString S1 =Sc.nextline (); String S2=Sc.nextline (); System.out.println ("S1:" + s1 + ", S2:" +S2); System.out.println ("-------------------"); //get a string first, get an int valueString s3 =Sc.nextline (); intB2 =Sc.nextint (); System.out.println ("S1:" + s3 + ", B:" +B2); System.out.println ("-------------------"); //get an int value first, get a string, get the data wrong        intA2 =Sc.nextint (); String S4=Sc.nextline (); System.out.println ("A:" + a2 + ", S2:" +S4); System.out.println ("-------------------"); //Workaround: Define two scanner objects and get two data respectively        intA =Sc.nextint (); Scanner SC2=NewScanner (system.in); String s=Sc2.nextline (); System.out.println ("A:" + A + ", S:" +s); }}

Java Api--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.