problems on ordinaryChar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rubyhuang
    New Member
    • Dec 2007
    • 19

    problems on ordinaryChar

    why when i using ordinaryChar("! ") in my java file, it still count it as a word.
    Code:
    file = new FileReader(filename);
    st = new StreamTokenizer(new BufferedReader(file));
    st.ordinaryChar('!');
    anyone can help me, thank you.
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Hey there!

    I am pretty new to this myself, and thanks for adding code tags... to tell you the truth though, I probably could have helped you if the entire code was posted; not savvy enough to tell what you need to do with just that bit of code. Let's have a look at the whole code..

    Rest assured, I'll yell for help for you if I cannot see what you're asking, what I know is peanuts compared to a lot of our folks here, so you're in good hands. Add code in and see what happens.

    In a bit!

    Dököll

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      By default, ! is an ordinary character, not a word character, so setting it to ordinary char doesn't change anything. Demo:

      [CODE=Java]import java.io.*;

      public class StreamTokenizer Example {
      public static void main(String[] args) throws IOException {
      String data = "abc! def";
      test(data, false);
      test(data, true);
      }

      static void test(String data, boolean setOrdinaryChar ) throws IOException {
      StreamTokenizer s = new StreamTokenizer (new StringReader(da ta));
      if (setOrdinaryCha r) s.ordinaryChar( '!');

      for(int token; (token = s.nextToken()) != StreamTokenizer .TT_EOF; ) {
      String output = "??";
      if (token == StreamTokenizer .TT_WORD) {
      output = s.sval;
      } else if (token == StreamTokenizer .TT_NUMBER) {
      output = String.valueOf( s.nval);
      } else if (token == StreamTokenizer .TT_EOL) {
      output = "EOL";
      } else {
      output = String.valueOf( (char)token);
      }
      System.out.prin tln(output);
      }
      System.out.prin tln();
      }
      }[/CODE]

      Comment

      Working...