Scanner in Java

Source: Internet
Author: User

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.

One, 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
*
* @author leizhimin 2009-7-24 11:24:47
*/
PublicclassTestscanner {
PublicStaticvoidMain (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);
}
}
}


Please enter a string:
234
>>>234
Wer
>>>wer
Bye
>>>bye
Exit

Process finished with exit code 0


write here first, have time to continue to improve.

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.

You can build scanner objects directly from strings (readable), input streams, files , and so on, and with scanner, you can scan the entire text piecemeal (according to regular separators), and make the desired processing of the results after the scan.

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

Use the default space separator:
PublicStaticvoidMain (string[] args)throwsFileNotFoundException {
Scanner s =NewScanner ("123 asdf SD 789 SDF asdfl,sdf.sdfl,asdf ... asdfkl las");
//S.usedelimiter ("|,|\\.");
while(S.hasnext ()) {
System.out.println (S.next ());
}
}


123
Asdf
Sd
45
789
Sdf
Asdfl,sdf.sdfl,asdf
... asdfkl
Las

Process finished with exit code 0


Remove the comment lines, using spaces or commas or dots as separators, and the output is as follows:
123
Asdf
Sd
45
789
Sdf
Asdfl
Sdf
Sdfl
Asdf







Asdfkl

Las

Process finished with exit code 0



four or one large stacks of API functions, few practical

(many APIs, comments are confusing, almost useless, this class has been ruined, a very good name, actually do all the dirty things)

Here are some relatively practical:

delimiter ()
Returns the Pattern that this Scanner is currently using to match the delimiter.
Hasnext ()
Determines whether the next paragraph still exists after the current scan position in the scanner. (The comments of the original apidoc are very nonsense)
Hasnextline ()
Returns true if there is another row in the input of this scanner.
Next ()
Finds and returns the next complete tag from this scanner.
Nextline ()
This scanner executes the current line and returns the input information that was skipped.


Five, progressive scan of the file, and output line by row

the scanning process of not seeing the value
PublicStaticvoidMain (string[] args)throwsFileNotFoundException {
InputStream in =NewFileInputStream (NewFile ("C:\\autosubmit.java"));
Scanner s =NewScanner (in);
while(S.hasnextline ()) {
System.out.println (S.nextline ());
}
}


Package own;

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import java.net.HttpURLConnection;
Import java.net.ProtocolException;
Import Java.net.URL;

Import Com.verisign.uuid.UUID;

/**
* @author Wangpeng
*
*/
public class Autosubmit {

/**
* @param args
* @throws Exception
*/
public static void Main (string[] args) throws Exception {

... Omit n rows here

Process finished with exit code 0


Java is still weak for string support, although Java has been working on it.

Scanner in Java

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.