views:

60

answers:

2

I have a stringtoList ArrayList that needs to return tokens from a StreamTokenizer but the s.sval is not compiling at run-time, can anyone help me with this problem:

private List<Token> stringToList(final String string) {
  // TODO your job
  // follow Main.main but put the tokens into a suitable list

  ArrayList<Token> al = new ArrayList<Token>(); 

  String word = "";

  String in = string;
  StreamTokenizer s = new StreamTokenizer(new StringReader(in));

  int token;
  while ((token = s.nextToken()) != StreamTokenizer.TT_EOF) {
   if (token == StreamTokenizer.TT_WORD) {
    DefaultToken t = (s.sval, s.lineno()); //problem here, not reading the sval from the StreamTokenizer!!!
    al.add(t);
  }
  return al;
  }
 }

Any help would be most appreciated

+2  A: 

How about:

String txt = "The quick brown fox jumps over the lazy dog";
String[] arr = txt.split("\\W");
List<String> list = Arrays.asList(arr);

Should work on Java 1.5+

rhu
+1  A: 

The following is not valid Java syntax:

DefaultToken t = (s.sval, s.lineno());

Without knowing what DefaultToken class looks like it will be hard for me to help, but perhaps you meant something like this?

DefaultToken t = new DefaultToken(s.sval, s.lineno());
Chris Knight