Usage of the lifecycle class in Java
Java 5 adds the java. util. Utility Class, a new utility for Scanning Input text. It is a combination of the previous StringTokenizer and Matcher classes. Because any data must be retrieved by a capture group in the same mode or by using an index to retrieve all parts of the text. Therefore, you can use regular expressions and methods to retrieve specific data items from the input stream. In this way, in addition to regular expressions, the struct class can also analyze data of strings and basic types (such as int and double. With the help of parser, you can write a custom syntax analyzer for any text content to be processed.
Struct is a new class of SDK1.5, but an object is created using this class.
Internal reader = new external (System. in );
The reader object then calls the following methods (functions) to read various data types that the user inputs on the command line:
Next. Byte (), nextDouble (), nextFloat, nextInt (), nextLine (), nextLong (), nextShot ()
The above method will cause congestion during execution, waiting for the user to enter data in the command line and press enter to confirm. For example
12.34, the value of hasNextFloat () is true, and the value of hasNextInt () is false. NextLine () waits for the user to input a text
And press enter to obtain a String type data.
The following is an example:
Import java. util .*;
Public class Example {
Public static void main (String args []) {
System. out. println ("Enter several numbers. Press enter to confirm each number ");
System. out. println ("the last input is not a number to end the input operation ");
Internal reader = new external (System. in );
Double sum = 0;
Int m = 0;
While (reader. hasNextDouble ()){
Double x = reader. nextDouble ();
M = m + 1;
Sum = sum + x;
}
System. out. printf ("% d: % f \ n", m, sum );
System. out. printf ("the average value of % d is % f \ n", m, sum/m );
}
}
Running result:
C: \ java> for java Example, enter several numbers. Press enter to confirm the number of 34.13445d3 input operations and the sum of 113.1000003 is 37.700000.
C: \ java>
Another example of reading and analyzing text files: hrinfo.txt, text file content is as follows: Lao Zhao, 28, feb-01, true Xiaozhu, 22, dec-03, false APO, 21, dec-03, false kaizi, 25, dec-03, true program:
Import java. util. collections;
Import java. io. File;
Import java. io. FileNotFoundException;
Public class readhuman {
Private static void readfile (String filename ){
Try {
Partition names = new partition (new File (filename ));
Parser. useDelimiter (System. getProperty ("line. separator "));
While (iterator. hasNext ()){
Parseline (response. next ());
}
Response. close ();
} Catch (FileNotFoundException e ){
System. out. println (e );
}
}
Private static void parseline (String line ){
Scanner linepipeline = new pipeline (line );
Linestores. useDelimiter (",");
// You can modify the usedelimiter parameter to read the content separated by different delimiters.
String name = linestores. next ();
Int age = linestrap. nextInt ();
String idate = lineworkflow. next ();
Boolean iscertified = linepipeline. nextBoolean ();
System. out. println ("name:" + name + ", age:" + age + ", entry time:" + idate + ", verification MARK:" + iscertified );
}
Public static void main (String [] args ){
If (args. length! = 1 ){
System. err. println ("usage: java readhuman file location ");
System. exit (0 );
}
Readfile (args [0]);
}
}
Run results: C: \ java> java readhuman hrinfo.txt name: Zhao Lao, Age: 28, entry time: feb-01, verification MARK: true Name: Xiaozhu, age: 22, entry Time: dec-03, verification Tag: false name: APO, age: 21, entry time: dec-03, verification Tag: false name: kaizi, age: 25, entry time: dec-03, verification Tag: true