Java.util.Scanner Application details + + Scan console input

Source: Internet
Author: User

Java.util.Scanner Application ExplanationJava.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 inputThis 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 {
Public StaticvoidMain (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 this here first, and then continue to improve when you are free. 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 a string ( readable), input streams, files, and so on to directly build scanner objects, with scanner, you can segment (according to regular delimited) to scan the entire text, and the results after the scan to do the desired processing. scanner default uses spaces as delimiters to separate text, but allows you to specify a new delimiterUse 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 removes the comment line, using a space or comma or dot as the delimiter, 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(A lot of APIs, comments are confusing, almost useless, this class is ruined, Kai a very good name, actually do all the dirty things) below these several 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 rowThe 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;

/**
* ???????????????????????????????? Y?????????? ¡?
* @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 support for strings is still relatively weak, although Java has been working hard. Java does Old TrendHas come down, more and more huge bloated, the past glory is becoming a stumbling block in Java, in order to backward compatibility, in order to smooth over, have to do a lot of painful choices. If Java can directly out of Java III, completely discard the existing dross, new design syntax and style. Java may continue to be brilliant.

Java.util.Scanner Application details + + Scan console input

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.