gpt4 book ai didi

java - 字符串到列表问题

转载 作者:行者123 更新时间:2023-12-02 00:52:12 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com