Java Stream tokenizer Use

Source: Internet
Author: User

Note: using Java to solve the problem generally with the scanner class to enter , but the time requirements of strict questions, with it may be timed out, I, when the solution POJ1823 encountered such problems, and then use Streamtokenizer class to do input , it's over. It seems that the latter is more efficient at handling inputs .
The summary is as follows:

1. Class Java.io.StreamTokenizer can get the input stream and parse it into token (token).
Streamtokenizer Nexttoken method reads the next tag

2. By default, Streamtokenizer believes that the following are tokens: letters, numbers, symbols other than the C and C + + comment symbols.
If the symbol "/" is not token, the content after the comment is not, and "/" is token. Single and double quotes, and their total content, can only be counted as a token.

3, in order to improve efficiency, using BufferedReader, as below, create Streamtokenizer object

Streamtokenizer St =new Streamtokenizer (new BufferedReader (New InputStreamReader (system.in)));


4, in order to get the token from the stream, you can call Streamtokenizer's Nexttoken () method.
After calling the Nexttoken () method, if the token is a string, the string S=st.sval can be used, if the integer is st.nval with an int n= (int).

St.nexttoken ();

int i= (int) st.nval; ST.NAVL The default parsing format is double

St.nexttoken ();

int j= (int) st.nval;

St.nexttoken ();

String S=st.sval;

Appendix:

Reproduced in: http://i.cnblogs.com/EditPosts.aspx?opt=1

Key points:

      • Class Java.io.StreamTokenizer can get the input stream and parse it into tokens (tokens). The Nexttoken method of Streamtokenizer will read the next tag
      • By default, Streamtokenizer believes that the following are tokens: letters, numbers, symbols other than the C and C + + comment symbols. If the symbol "/" is not token, the content after the comment is not, and "\" is the token. Single and double quotes, and the contents, can only be considered a token.
      • To count the number of characters in a file, you cannot simply count the number of tokens, because the number of characters is not equal to token, and the content in the quotation marks is a token, even if it is 10 pages, according to token. If you want the quotes and quotes to count as tokens, you should use the Streamtokenizer Ordinarycha () method to treat both single and double quotes as ordinary characters.

The file input path is specified directly in the program: String fileName = "D:/ceshi.txt";

Package szu.edu;

Import Java.io.BufferedReader;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.StreamTokenizer;

/**
* Use Streamtokenizer to count the number of characters in a file
* The Streamtokenizer class gets the input stream and parses it into "tags", allowing one tag to be read at a time.
* The analysis process is controlled by a table and many flags that can be set to various states.
* The markup generator for this stream can recognize identifiers, numbers, referenced strings, and various annotation styles.
*
* By default, Streamtokenizer believes the following are tokens: letters, numbers, symbols other than C and C + + comment symbols.
* If the symbol "/" is not token, the content after the comment is not, and "\" is token. Single and double quotes, and the contents, can only be considered a token.
* Statistics article character number of the program, not simple statistical token number is all right, because the number of characters is not equal to token. In accordance with the provisions of token,
* The content in the quotation marks is a token even if it is 10 pages. If you want the quotes and quotes to count as tokens, you should call the following code:
* St.ordinarychar (' \ '); Set single quotation marks to normal characters
* St.ordinarychar (' \ "'); Set double quotation marks to normal characters
*/
public class Statisfilechars {

/**
* Statistics of characters
* @param filename
* @return Number of characters
*/
public static long Statis (String fileName) {

FileReader filereader = null;
try {
FileReader = new FileReader (fileName);
To create a markup generator that parses a given character stream
Streamtokenizer st = new Streamtokenizer (New BufferedReader (
FileReader));

The Ordinarychar method specifies that the character parameter is a "normal" character in this markup generator.
The following specifies that single quotation marks, double quotes, and comment symbols are ordinary characters
St.ordinarychar (' \ ');
St.ordinarychar (' \ ');
St.ordinarychar ('/');

String s;
int numbersum = 0;
int wordSum = 0;
int symbolsum = 0;
int total = 0;
The //nexttoken method reads the next token.
//tt_eof indicates constants that have been read to the end of the stream.
while (St.nexttoken ()! = streamtokenizer.tt_eof) {
After calling the NextToken method, the Ttype field will contain the type of tag just read
switch (st.ttype) {
//tt_eol indicates a constant that has been read to the end of a line.
Case STREAMTOKENIZER.TT_EOL:
Break
//tt_number indicates a constant that has been read to a numeric tag
Case Streamtokenizer.tt_number:
If the current tag is a number, the Nval field will contain the value of that number
s = string.valueof ((st.nval));
System.out.println (s);
Numbersum + = S.length ();
Break
//tt_word indicates a constant that has been read to a literal tag
Case Streamtokenizer.tt_word:
If the current tag is a text marker, the Sval field contains a string that gives the character of the text tag
s = st.sval;
WordSum + = S.length ();
Break
Default
If the above 3 types are not, then the punctuation marks in English
s = string.valueof ((char) st.ttype);
Symbolsum + = S.length ();
}
}
System.out.println ("Sum of number =" + numbersum);
System.out.println ("sum of Word =" + wordSum);
System.out.println ("sum of symbol =" + symbolsum);
Total = symbolsum + numbersum + wordSum;
SYSTEM.OUT.PRINTLN ("total =" + total);
return total;
} catch (Exception e) {
E.printstacktrace ();
return-1;
} finally {
if (FileReader! = null) {
try {
Filereader.close ();
} catch (IOException E1) {
}
}
}
}

public static void Main (string[] args) {
String fileName = "D:/ceshi.txt";
Statisfilechars.statis (FileName);
}
}

Note: In addition to the above usage, there is a very common usage. For basic input operation! (For example: in the common language ACM programming, the input efficiency is higher.) )

BufferedReader provides quite fast read operations for almost all problems. It is used to read a characters and lines only. To read tokens and numbers your should use StringTokenizer orstreamtokenizer.

Import java.io.*;p ublic class main{public    static void Main (string[] args) throws IOException    {        Streamtokenizer in = new Streamtokenizer (new BufferedReader (New InputStreamReader (system.in)));        PrintWriter out = new PrintWriter (new OutputStreamWriter (System.out));        int a, B;        While (In.nexttoken ()! = streamtokenizer.tt_eof)        {            a = (int) in.nval;            In.nexttoken ();            b = (int) in.nval;            Out.println (A + b);            System.out.println ("a + b =" + (a+b));        }        Out.flush ();    }}

Java Stream tokenizer Use

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.