skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, January 16, 2012

Create a Class defining an Object that Parses Each line of Input

Posted by Admin at 9:07 AM – 0 comments
 
Create a class defining an object that parses each line of input from the keyboard that contains items separated by an arbitrary delimiter (for example, a colon, or a comma, or a forward slash, and so on) and return the items as an array of type String[]. For example, the input might be

1/one/2/two

The output would be returned as an array of type String[] containing "1", "one", "2", "two".

import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class ParseInput {
  private StreamTokenizer tokenizer;
  private char separator;

  // Constructor requires the separator character as the argument
  public ParseInput(char separator){
    this.separator = separator;
    tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    setTokenizerState();
  }

  // Sets the tokenizer to accept anyhing except the separator and end-of-line as
  // part of a word
  private void setTokenizerState() {
    tokenizer.resetSyntax();
    tokenizer.wordChars('\u0000', (char)(separator - 1));                 // Everything is a word character
    tokenizer.wordChars((char)(separator + 1), '\u00ff');                 // except for the separator
    tokenizer.whitespaceChars('\n', '\n');                                // Make end-of-line whitespace(and therefore a word delimiter)
    tokenizer.whitespaceChars(separator, separator);                      // Delimiter passed to constructor seaparates words
    tokenizer.eolIsSignificant(true);                                     // End-of-line to be reported as TT_EOL

  }

  // Read tokens from the input
  public String[] readTokens() {
    int type = 0;                                                         // Store the value returned by nextToken()
    int strCount = 5;                                                     // String capacity increment
    String[] tokens = new String[strCount];                               // Will store the tokens that we find
    int nTokens = 0;                                                      // Number of tokens stored
    try {
      while((type = tokenizer.nextToken()) != StreamTokenizer.TT_EOL) {   // As long as we don'y have EOL
        // Verify we have space in the tokens array
        if(nTokens == tokens.length) {
          String[] newTokens = new String[tokens.length+strCount];        // tokens array is full so create a larger array

          // Copy old array contents to new array
          for(int i = 0 ; i < nTokens ; ++i) {
            newTokens[i] = tokens[i];
          }
          tokens = newTokens;                                             // Replace old array by new array
        }
        if(type == StreamTokenizer.TT_WORD) {                             // Check for a word
          tokens[nTokens++] = tokenizer.sval;                             // and save it in the vector
        } else {
          assert false;                                                   // We only expect words
        }

      }
      // Store tokens in array of exactly the right size
      if(nTokens < tokens.length) {
        String[] newTokens = new String[nTokens];
        // Copy old array contents to new array
        for(int i = 0 ; i < nTokens ; ++i) {
          newTokens[i] = tokens[i];
        }
        tokens = newTokens;                                               // Replace old array by new array
      }
    } catch(IOException e) {
      e.printStackTrace();
      System.exit(1);
    }

    return tokens;
  }

  // Convenience method to change the separator
  public void setSeparator(char separator) {
    this.separator = separator;
    setTokenizerState();
  }
}

This is the main Class to Test Above Java Class ' ParseInput '.

public class TestParseInput {
  public static void main(String[] args) {
    char separator = ',';
    ParseInput tokenizer = new ParseInput(separator);
    parseInputLine(tokenizer, separator);

    // Try a different separator
    separator = '/';
    tokenizer.setSeparator(separator);
    parseInputLine(tokenizer, separator);
  }

  // Reusable parse input operation
  static void parseInputLine(ParseInput tokenizer, char separator) {
    System.out.println("\nEnter items separated by " + separator + ":");
    String[] inputs = tokenizer.readTokens();

    if(inputs.length != 0) {
      System.out.println("The items that you entered are:");
      for( String input : inputs) {
        System.out.print("  " + input);
      }
    }
  }
}


Output of Above Java Program
Enter items separated by ,:
java, programs, and, examples, blog
The items that you entered are:
java programs and examples blog
Enter items separated by /:
java/programs/and/example/blog
The items that you entered are:
java programs and example blog

Labels: Java Class Examples , StreamTokenizer Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger