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<开发者_如何学CToken>();
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
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+
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());
精彩评论