我有一个 stringtoList ArrayList 需要从 StreamTokenizer 返回 token ,但 s.sval 没有在运行时编译,任何人都可以帮助我解决这个问题:
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;
}
}
任何帮助将不胜感激
最佳答案
怎么样:
String txt = "The quick brown fox jumps over the lazy dog";
String[] arr = txt.split("\\W");
List<String> list = Arrays.asList(arr);
应该适用于 Java 1.5+
关于java - 字符串到列表问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2519474/