UserPreferences

JavaParse


1. Java Parse

This example shows how you can take a string (called line) and break it up into pieces. For the example below, consider that line = "thisBREAKisBREAKit". Then the first call to nextToken() would be "this". The next call to nextToken() would be "is". And the last call to nextToken() would be "it"

import java.util.*;

public class TestParse {
    public static void main(String args[]) {
        // get the next line
        while (not eof) {
            StringTokenizer tokenizer = null;
            String command = null;
            try {
                tokenizer = new StringTokenizer(line, "BREAK");
                command = tokenizer.nextToken();
            } catch (NullPointerException e) {
                command = "";
            } catch (NoSuchElementException e) {
                command = "";
            }
            if (command.equals("some string")) {
                String nextString = tokenizer.nextToken().trim();
            } else if (command.equals("some other string")) {
                String nextString1 = tokenizer.nextToken().trim();
                String nextString2 = tokenizer.nextToken().trim();
            } ... {
            } else {
                // error condition
            }
        }
    }
}