Example usage for java.io StreamTokenizer commentChar

List of usage examples for java.io StreamTokenizer commentChar

Introduction

In this page you can find the example usage for java.io StreamTokenizer commentChar.

Prototype

public void commentChar(int ch) 

Source Link

Document

Specified that the character argument starts a single-line comment.

Usage

From source file:StreamTokenApp.java

public static void main(String args[]) throws IOException {
    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer inStream = new StreamTokenizer(inData);
    inStream.commentChar('#');
    boolean eof = false;
    do {/*from w w w .j a v  a  2s.c om*/
        int token = inStream.nextToken();
        switch (token) {
        case inStream.TT_EOF:
            System.out.println("EOF encountered.");
            eof = true;
            break;
        case inStream.TT_EOL:
            System.out.println("EOL encountered.");
            break;
        case inStream.TT_WORD:
            System.out.println("Word: " + inStream.sval);
            break;
        case inStream.TT_NUMBER:
            System.out.println("Number: " + inStream.nval);
            break;
        default:
            System.out.println((char) token + " encountered.");
            if (token == '!')
                eof = true;
        }
    } while (!eof);
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer inStream = new StreamTokenizer(inData);
    inStream.commentChar('#');
    boolean eof = false;
    do {/*from  w w w  . jav a  2 s  .c om*/
        int token = inStream.nextToken();
        switch (token) {
        case StreamTokenizer.TT_EOF:
            System.out.println("EOF encountered.");
            eof = true;
            break;
        case StreamTokenizer.TT_EOL:
            System.out.println("EOL encountered.");
            break;
        case StreamTokenizer.TT_WORD:
            System.out.println("Word: " + inStream.sval);
            break;
        case StreamTokenizer.TT_NUMBER:
            System.out.println("Number: " + inStream.nval);
            break;
        default:
            System.out.println((char) token + " encountered.");
            if (token == '!')
                eof = true;
        }
    } while (!eof);
}

From source file:com.zimbra.common.calendar.ZoneInfo2iCalendar.java

private static void readExtraData(Reader reader) throws IOException, ParseException {
    char dquote = '"';
    StreamTokenizer tokenizer = new StreamTokenizer(reader);
    tokenizer.resetSyntax();/* w  w w.  j av  a 2  s.c o m*/
    tokenizer.wordChars(32, 126);
    tokenizer.whitespaceChars(' ', ' ');
    tokenizer.whitespaceChars('\t', '\t');
    tokenizer.whitespaceChars(0, 20);
    tokenizer.commentChar('#');
    tokenizer.quoteChar(dquote);
    tokenizer.eolIsSignificant(true);

    List<String> tokenList = new ArrayList<String>();
    LineType lineType = LineType.UNKNOWN;
    boolean atLineStart = true;

    int ttype;
    int prevTtype = StreamTokenizer.TT_EOL; // used for empty line detection
    while ((ttype = tokenizer.nextToken()) != StreamTokenizer.TT_EOF) {
        int lineNum = tokenizer.lineno();
        if (ttype == StreamTokenizer.TT_WORD || ttype == dquote) {
            String token = tokenizer.sval;
            if (atLineStart) {
                lineType = LineType.lookUp(token);
                if (LineType.UNKNOWN.equals(lineType))
                    throw new ParseException("Invalid line type", lineNum);
            } else {
                tokenList.add(token);
            }
            atLineStart = false;
        } else if (ttype == StreamTokenizer.TT_EOL) {
            if (prevTtype == StreamTokenizer.TT_EOL) {
                prevTtype = ttype;
                continue;
            }
            atLineStart = true;
            switch (lineType) {
            case PRIMARYZONE:
                if (tokenList.size() < 1)
                    throw new ParseException("Not enough fields in a PrimaryZone line", lineNum);
                String primaryTZID = tokenList.get(0);
                sPrimaryTZIDs.add(primaryTZID);
                break;
            case ZONEMATCHSCORE:
                if (tokenList.size() < 2)
                    throw new ParseException("Not enough fields in a ZoneMatchScore line", lineNum);
                String zoneName = tokenList.get(0);
                String zoneMatchScoreStr = tokenList.get(1);
                int zoneMatchScore = 0;
                try {
                    zoneMatchScore = Integer.parseInt(zoneMatchScoreStr);
                } catch (NumberFormatException e) {
                    throw new ParseException("Zone match score must be an integer: " + zoneMatchScoreStr,
                            lineNum);
                }
                sMatchScores.put(zoneName, zoneMatchScore);
                break;
            }
            if (atLineStart) {
                tokenList.clear();
                lineType = LineType.UNKNOWN;
            }
        } else if (ttype == StreamTokenizer.TT_NUMBER) {
            // shouldn't happen
            throw new ParseException("Invalid parser state: TT_NUMBER found", lineNum);
        }
        prevTtype = ttype;
    }
}