[英]Stringtokenizer Issue
I have the following line: 我有以下几行:
1675 | 29/02/2016 | Jane Smith | James C | James C | 4 | 3
Here's my string tokenizer code: 这是我的字符串标记器代码:
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();
However, I keep getting the no such element exception. 但是,我一直没有此类元素异常。 What's wrong with my code?
我的代码有什么问题?
Try this: 尝试这个:
String line = "1675 | 29/02/2016 | Jane Smith | James C | James C | 4 | 3";
String parts[] = line.split(Pattern.quote(" | "));
The result is an array of String parts[0] .. parts[5]
结果是一个字符串
parts[0] .. parts[5]
的数组, parts[0] .. parts[5]
是string parts[0] .. parts[5]
Check it out with the output System.out.println(Arrays.toString(parts));
使用输出
System.out.println(Arrays.toString(parts));
[1675, 29/02/2016, Jane Smith, James C, James C, 4, 3]
Why you are using StringTokenizer
class why dont you use split();
为什么使用
StringTokenizer
类,为什么不使用split();
method of String
class. String
类的方法。
String abc ="your | string | should | be | here ";
String[] strArr = abc.split(" | ");
/// loop on strArr to get your tokens.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.