我有以下几行:
1675 | 29/02/2016 | Jane Smith | James C | James C | 4 | 3
这是我的字符串标记器代码:
StringTokenizer stringTokenizer = new StringTokenizer(line, " | ");
int cId = Integer.parseInt(stringTokenizer.nextElement().toString());
String cDate = stringTokenizer.nextElement().toString();
String cName1 = stringTokenizer.nextElement().toString();
String cName2 = stringTokenizer.nextElement().toString();
String cName3 = stringTokenizer.nextElement().toString();
String cName4 = stringTokenizer.nextElement().toString();
String wName = stringTokenizer.nextElement().toString();
String wName2 = stringTokenizer.nextElement().toString();
String cScore1 = stringTokenizer.nextElement().toString();
String cScore2 = stringTokenizer.nextElement().toString();
但是,我一直没有此类元素异常。我的代码有什么问题?
最佳答案
尝试这个:
String line = "1675 | 29/02/2016 | Jane Smith | James C | James C | 4 | 3";
String parts[] = line.split(Pattern.quote(" | "));
结果是字符串
parts[0] .. parts[5]
的数组使用输出
System.out.println(Arrays.toString(parts));
进行检查[1675, 29/02/2016, Jane Smith, James C, James C, 4, 3]
关于java - Stringtokenizer问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35684178/