Use of the Scanner class

Source: Internet
Author: User

Use of the Scanner class

(Accept keyboard input)

Java.util.Scanner is a new feature of JAVA5, and the main function is to simplify text scanning. The most practical part of this class is to get console input, other features are very chicken, although the Java API documentation lists a number of API methods, but not very much.

"Code Sample"

1  Public Static voidMain (string[] args) {2 test01 ();3 test02 ();4     }5 6      Public Static voidtest02 () {7Scanner s=NewScanner (system.in);8System.out.println ("Please enter a addend:");9         intA=s.nextint ();TenSystem.out.println ("Please enter Summand:"); One         intb=s.nextint (); A         intsum=a+b; -System.out.println ("calculated results, and as:" +sum); -     } the  -      Public Static voidtest01 () { -System.out.println ("Please Enter:"); -Scanner s=NewScanner (system.in);//system.in: Keyboard input +String Str=s.next ();//The program runs to next will block, waiting for keyboard input! Next returns a string -System.out.println ("Just keyboard entered:" +str); +}
View CodeOne, scan console input

This example is often used, but if you don't have a scanner, you'll know how uncomfortable it is to write.
When a Scanner is created through new Scanner (system.in), the console waits for input until the hit key is finished and the input is passed to Scanner as the scanned object. If you want to get the input, you only need to call Scanner's Nextline () method.

/*** Scan Console input * *@authorleizhimin 2009-7-24 11:24:47*/  Public classTestscanner { Public Static voidMain (string[] args) {Scanner s=NewScanner (system.in); System.out.println ("Please enter a string:");  while(true) {String line=S.nextline (); if(Line.equals ("Exit")) Break; System.out.println (">>>" +Line ); }         } }

Console output:

234 >>>234>>>>>>0
Second, if the scanner is easy to use, it is better to say that scanner's constructor supports a variety of ways, the construction of scanner objects is very convenient.

can be from strings (readable), input streams, files, and so on to build scanner objects directly , with the scanner, you can scan the entire text by paragraph (according to the regular divider) and do the desired processing of the scanned results.

scanner default uses spaces as delimiters to separate text, but allows you to specify a new delimiter

Use the default space separator:

   Public Static void throws filenotfoundexception {                 new Scanner ("123 asdf SD 789 SDF asdfl,sdf.sdfl,asdf    ... asdfkl    las "//                while                  (S.hasnext ()) {                         System.out.println (S.next ());                 }         }

Console output:

1237890

Remove the comment lines, using spaces or commas or dots as separators, and the output is as follows:

1237890

Use of the Scanner class

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.