Connecting Tech Pros Worldwide Help | Site Map

problems on ordinaryChar

Newbie
 
Join Date: Dec 2007
Posts: 19
#1: May 13 '08
why when i using ordinaryChar("!") in my java file, it still count it as a word.
Expand|Select|Wrap|Line Numbers
  1. file = new FileReader(filename);
  2. st = new StreamTokenizer(new BufferedReader(file));
  3. st.ordinaryChar('!');
anyone can help me, thank you.
Dököll's Avatar
Moderator
 
Join Date: Nov 2006
Location: Upstate NY - US
Posts: 2,267
#2: Jun 12 '08

re: problems on ordinaryChar


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
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#3: Jun 12 '08

re: problems on ordinaryChar


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

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class StreamTokenizerExample {
  4.     public static void main(String[] args) throws IOException {
  5.         String data = "abc! def";
  6.         test(data, false);
  7.         test(data, true);
  8.     }
  9.  
  10.     static void test(String data, boolean setOrdinaryChar) throws IOException {
  11.         StreamTokenizer s = new StreamTokenizer(new StringReader(data));
  12.         if (setOrdinaryChar) s.ordinaryChar('!');
  13.  
  14.         for(int token; (token = s.nextToken()) != StreamTokenizer.TT_EOF; ) {
  15.             String output = "??";
  16.             if (token == StreamTokenizer.TT_WORD) {
  17.                 output = s.sval;
  18.             } else if (token == StreamTokenizer.TT_NUMBER) {
  19.                 output = String.valueOf(s.nval);
  20.             } else if (token == StreamTokenizer.TT_EOL) {
  21.                 output = "EOL";
  22.             } else {
  23.                 output = String.valueOf((char)token);
  24.             }
  25.             System.out.println(output);
  26.         }
  27.         System.out.println();
  28.     }
  29. }
Reply