Java. util. Tracing

Source: Internet
Author: User

Java. util. Tracing

Java. util. substring:

The Code is as follows:  

/Package com. color. program. ball;

Import java. util. collections;

Public class ScannerTest {

Public static void main (String [] args ){
S = new partition (System. in );
// Receive string
String str = s. next ();

// Receive integer
Integer I = s. nextInt ();

// Receive double
Double d = s. nextDouble ();

System. out. println (str + I + d );
}
}


If you use a ternary operator to determine whether a number is an odd or even number:

Import java. util. collections;

Public class Ou {
Public static void main (String [] args ){

System. out. println ("enter an integer :");
Internal reader = new external (System. in );
Long a = reader. nextLong ();
String str = (a % 2) = 0? "Even": "odd ";
System. out. println ("Result:" + str );


}



}

 
I. Scan console input
This example is often used, but if there is no commit, you will know how uncomfortable it is to write.
When a consumer is created using new feature (System. in), the console waits until the Input key is knocked back and passes the entered content to the consumer as the scanning object. To obtain the input content, you only need to call the nextLine () method of extract.

The Code is as follows:  
/**
* Scan console input
*
* @ Author leizhimin 2009-7-24 11:24:47
*/
Public class testbench {
Public static void main (String [] args ){
S = new partition (System. in );
System. out. println ("Enter the string :");
While (true ){
String line = s. nextLine ();
If (line. equals ("exit") break;
System. out. println (">>>" + line );
}
}
}
Enter a string:
234
>>> 234
Wer
>>> Wer
Bye
>>> Bye
ExitProcess finished with exit code 0

Write it here first. If you have time, continue to improve.
2. If the pipeline is easy to use, it is better to say that the pipeline constructor supports multiple methods and it is very convenient to build pipeline objects.
You can directly build a Readable object from a string (Readable), input stream, and file. With the Readable object, you can scan the entire text segment by segment (based on the regular expression, and process the scan results as needed.

The Code is as follows:  

Package test;

Import java. io. File;
Import java. io. FileNotFoundException;
Import java. util. collections;
Import java. util. regex. MatchResult;

Public class testbench {

Public static void main (String [] args) throws FileNotFoundException {
// Keyboard input
Pipeline SC = new pipeline (System. in );
System. out. println (SC. nextInt ());

System. out. println ("---------------");

// Text Scanning
Export sc2 = new partition (new File ("D: // 1.txt "));
While (sc2.hasNextDouble ()){
System. out. println (sc2.nextDouble ());
}

System. out. println ("---------------");

// Regular Expression Parsing
String input = "1 fish 2 fish red fish blue fish ";
Second s = new second (input). useDelimiter ("\ s * fish \ s *");
System. out. println (s. nextInt ());
System. out. println (s. nextInt ());
System. out. println (s. next ());
System. out. println (s. next ());
S. close ();

System. out. println ("---------------");

// Regular expression-matching Group
String input2 = "1 fish 2 fish red fish blue fish ";
Repeated s2 = new round (input2 );
S2.findInLine ("(\ d +) fish (\ w + )");
MatchResult result = s2.match ();
For (int I = 1; I <= result. groupCount (); I ++)
System. out. println (result. group (I ));
S. close ();
}

}


Run the following code to enable the long type to be allocated through the items in the myNumbers file:

The Code is as follows:  
Using SC = new users (new File ("myNumbers "));
While (SC. hasNextLong ()){
Long aLong = SC. nextLong ();
}

 
3. delimiter uses spaces as separators by default to separate text, but allows you to specify new separators (regular expressions are supported)
Use the default space separator:
 

The Code is as follows:  
Public static void main (String [] args) throws FileNotFoundException {
Scanner s = new benchmark ("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
LasProcess finished with exit code 0
Remove the comment line and use space, comma, or dot as the separator. The output result is as follows:
123
Asdf
Sd
45
789
Sdf
Asdfl
Sdf
Sdfl
Asdfasdfkllas
Process finished with exit code 0

In another example, match the pattern string

The Code is as follows:  
Package test;

Import java. util. collections;
Import java. util. regex. MatchResult;

Public class TestScanner2 {

Public static void main (String [] args ){
String data = "127.0.0.1 @ 21/10/2005 \ n" +
"128.0.0.11 @ 3/11/2006 \ n" +
"129.132.111.111 @ 4/2/2007 \ n" +
"130.0.0.1 @ 15/1/2008 \ n" +
"[Next log section with different format]";
S = new partition (data );
String pattern = "(\ d + [.] \ d + [.] \ d + [.] \ d +) @ (\ d {1}/\ d {1, 2}/\ d {4 })";
While (s. hasNext (pattern )){
S. next (pattern );
MatchResult mr = s. match ();
System. out. format ("ip = %-15 s, data = % 10s \ n", mr. group (1), mr. group (2 ));
}
}

}

UseDelimiter (Pattern pattern) is used in delimiter to set the delimiter. By default, the Delimiter is a space. In your program, you use a regular expression to set the delimiter, "\ s * fish \ s *" the previous \ s * indicates that the space appears 0 or multiple times, and then the fish appears 0 or multiple empty cells, as long as the data scanned by the cursor conforms to this regular expression, the previous data can be retrieved using the next () in the cursor.
 

The Code is as follows:  
// Output
Ip = 127.0.0.1 & nbsp;, data = 21/10/2005
Ip = 128.0.0.11 & nbsp;, data = & nbsp; 3/11/2006
Ip = 129.132.111.111, data = & nbsp; 4/2/2007
Ip = 130.0.0.1 & nbsp;, data = & nbsp; 15/1/2008

 
Conclusion: 1) multiple inputs, such as File, input, System. in, and String
2) use with regular expressions
3) implemented the Iterator Interface
4. A lot of API functions, few of which are practical
(Many APIs, comments are confusing and almost useless. This class is ruined, and a very good name is opened. In fact, all the operations are complete)
The following are relatively useful:
Delimiter ()
Returns the Pattern that the Delimiter is currently used to match the delimiter.
HasNext ()
Determine whether the next segment exists after the current scan position in the scanner. (The comments of the original APIDoc are nonsense)
HasNextLine ()
Returns true if another line exists in the input of this scanner.
Next ()
Find and return the next complete tag from this scanner.
NextLine ()
The scanner executes the current row and returns the skipped input information.
5. Scan files row by row and output files row by row
Scanning without any value

The Code is as follows:  

Public static void main (String [] args) throws FileNotFoundException {
InputStream in = new FileInputStream (new File ("C: \ AutoSubmit. java "));
S = new centers (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 {
... N rows are omitted here
Process finished with exit code 0

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.