StreamTokenizer如何使用?

320 阅读2分钟

代码结构

image.png

构造方法

private StreamTokenizer() {
    // 一个单词(String)是通过多个单词要素(char)组成的
    wordChars('a', 'z'); // 将a-z的字符作为一个普通单词要素,即可以正常将这些字符解析
    wordChars('A', 'Z'); // 将A-Z的字符作为一个普通单词要素,即可以正常将这些字符解析
    wordChars(128 + 32, 255); // 将ASCII码表中160-255的字符作为一个普通单词要素,即可以正常将这些字符解析
    
    // StreamTokenizer默认认为空白符就是分隔符
    // 将ASCII码表中0-' '的字符作为空白分隔符,因为StreamTokenizer默认认为空白符就是分隔符,所以这些字符也就变成了分隔符
    whitespaceChars(0, ' ');
    
    commentChar('/'); // 将/字符作为注解符,也就是说当一个token中包括/时,/后面的字符全部不再解析

    quoteChar('"'); // 指定 " 为分隔符
    quoteChar('\''); // 指定 ' 为分隔符
    parseNumbers(); // 当stream tokenizer遭遇到一个单词为双精度的浮点数时,会把它当作一个数字,而不是一个单词。
}
private StreamTokenizer() {
    // All byte values 'A' through 'Z', 'a' through 'z', and '\u00A0' through '\u00FF' are considered to be alphabetic.
    wordChars('a', 'z');
    wordChars('A', 'Z');
    wordChars(128 + 32, 255);
    
    // All byte values '\u0000' through '\u0020' are considered to be white space.
    whitespaceChars(0, ' ');
    
    // '/' is a comment character
    commentChar('/');
    
    // Single quote '\'' and double quote '"' are string quote characters.
    quoteChar('"');
    quoteChar('\'');
    
    // Numbers are parsed.
    parseNumbers();
    
    // Ends of lines are treated as white space, not as separate tokens.
}

结束符

/**
 * A constant indicating that the end of the line has been read.
 */
public static final int TT_EOL = '\n';

/**
 * A constant indicating that the end of the stream has been read.
 */
public static final int TT_EOF = -1;

/**
 * A constant indicating that a number token has been read.
 */
public static final int TT_NUMBER = -2;

/**
 * A constant indicating that a word token has been read.
 */
public static final int TT_WORD = -3;

算法题常用以下套路

StreamTokenizer st =new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); 
 
st.nextToken();          // 获取下一组标记、默认是按照空格分割的、回车,tab是结束符 
int i=(int) st.nval;     //st.navl默认解析出的格式是double
 
st.nextToken();     
double j=st.nval; 
 
st.nextToken();     
String s=st.sval;