我正在使用String.split()
方法拆分 XML 元素。XML 元素的格式如下所述:
开始:23 | 停止:43 | 名称:abc def
拆分字符串后,我试图修剪和分配如下:
String[] parts = oneLine.split("\\s*\\|\\s*"); // splits into 3 strings
for (int x = 0; x < parts.length; x++) {
String tmp = parts[0];
if (tmp.startsWith("start:")) {
tmp = tmp.substring("start:".length());
try {
this.begin = Integer.parseInt(tmp);
}
catch (NumberFormatException nfe) {
nfe.printStackTrace();
}
}
else {
System.err.println("Wrong format");
}
String tmp1 = parts[1];
if ( tmp1.startsWith("stop:")) {
tmp1 = tmp1.substring("stop:".length());
try {
this.end = Integer.parseInt(tmp1);
}
catch ( NumberFormatException nfe ) {
nfe.printStackTrace();
}
}
else {
System.err.println("Wrong format"+tmp1);
}
Strint tmp2 = parts[2];
if ( tmp2.startsWith("name:")) {
tmp2 = tmp2.substring("name:".length());
try {
this.name = tmp2;
}
catch ( NumberFormatException nfe ) {
nfe.printStackTrace();
}
}
else {
System.err.println("Wrong format"+tmp2);
}
这将返回一个错误Wrong format
。这是作业的问题String tmp1 = parts[0]
吗?