Scanner in java

java.util.Scanner is a new feature of Java5, the main function is to simplify text scanning. The most practical part of this class is to obtain console input, other functions are very tasteless, although a large number of API methods are listed in the Java API documentation, but they are not very good.

1. Scan console input

This example is often used, but if there is no Scanner, you will know how uncomfortable it is to write.
When a Scanner is created through new Scanner(System.in), the console will wait for input until the Enter key is pressed, and the input content is passed to the Scanner as the scanning object. If you want to get the input content, you only need to call the Scanner's nextLine() method.

/** 
* Scan console input 

* @author leizhimin 2009-7-24 11:24:47 
*/ 
public class TestScanner { 
        public static void main(String[] args) { 
                Scanner s = new Scanner(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 it here first, and then continue to improve it when you have time.

Second, if Scanner is easy to use, it is better to say that Scanner's constructor supports a variety of methods, and it is very convenient to construct Scanner objects.

Scanner objects can be constructed directly from strings (Readable), input streams, files, etc. With Scanner, you can scan the entire text segment by segment (according to regular delimitation), and do what you want with the scanned results deal with.

3. Scanner uses spaces as separators to separate text by default, but allows you to specify new separators

to use the default space separators:
        public static void main(String[] args) throws FileNotFoundException { 
                Scanner s= new Scanner("123 asdf sd 45 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


will comment Lines are removed, using spaces or commas or periods as separators, the output is as follows:
123 
asdf 
sd 
45 
789 
sdf 
asdfl 
sdf 
sdfl 
asdf 







asdfkl 

las 

Process finished with exit code 0



4. A lot of API functions, but few are practical

(many APIs, comments are very confusing, almost useless, this class is just wasted, it has a very good name, actually It's all dirty.)

The following are relatively useful:

delimiter() 
          Returns the Pattern that this Scanner is currently using to match the delimiter.
hasNext() 
          determines whether there is a next segment after the current scan position in the scanner. (The original APIDoc comment was crap)
hasNextLine() 
          returns true if there is another line in this scanner's input.
next() 
          finds and returns the next complete token from this scanner.
nextLine() 
          This scanner executes the current line and returns the skipped input information.


5. Scan the file line by line, and output

the scanning process that cannot see the value line by line
        public static void main(String[] args) throws FileNotFoundException { 
                InputStream in = new FileInputStream(new File("C:\\AutoSubmit.java") ); 
                Scanner s = new Scanner(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 lines here 

Process finished with exit code 0


Java's support for strings is still relatively weak, although Java has been working on it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325288066&siteId=291194637