英文:
StringTokeniser not reading data
问题
import java.util.StringTokenizer;
public class rough {
public static void main(String[] args) throws Exception {
StringTokenizer itr = new StringTokenizer("PKE2324 02-12-2020 200"
+ "\nMJD432 19-05-2019 150");
while (itr.hasMoreTokens()){
String line = itr.nextToken().toString();
String[] tokens = line.split(" ");
String[] date = tokens[1].split("-");
String yr = date[2];
String reg = tokens[0];
String regy = new String(reg + " " + yr);
System.out.println(regy);
}
}
}
我想要获取注册号和年份作为一个字符串。当我运行这段代码时,我一直得到“ArrayIndexOutofBounds”错误。
英文:
I've to read data from a file and I've just entered some sample data as a String in StringTokenizer. I can't understand what is wrong with my code here. Can someone please advise?
import java.util.StringTokenizer;
public class rough {
public static void main(String[] args) throws Exception {
StringTokenizer itr = new StringTokenizer("PKE2324 02-12-2020 200"
+ "\nMJD432 19-05-2019 150");
while (itr.hasMoreTokens()){
String line = itr.nextToken().toString();
String[] tokens = line.split(" ");
String[] date = tokens[1].split("-");
String yr = date[2];
String reg = tokens[0];
String regy = new String(reg + " " + yr);
System.out.println(regy);
}
}
}
I want to get the registration number and year as a String. When I run this, I keep getting ArrayIndexOutofBounds
答案1
得分: 1
这是由于逻辑错误和不适当的分隔符引起的运行时错误类型。
假设您想使用换行符对大字符串进行分词,可以使用\n
作为StringTokenizer的分隔符。
请查看下面的已更正代码,它满足您的用例:
import java.util.StringTokenizer;
public class rough {
public static void main(String[] args) throws Exception {
StringTokenizer itr = new StringTokenizer("PKE2324 02-12-2020 200"
+ "\nMJD432 19-05-2019 150", "\n");
while (itr.hasMoreTokens()){
String line = itr.nextToken().toString();
String[] tokens = line.split(" ");
String[] date = tokens[1].split("-");
String yr = date[2];
String reg = tokens[0];
String regy = new String(reg + " " + yr);
System.out.println(regy);
}
}
}
输出:
PKE2324 2020
MJD432 2019
英文:
This is the type of runtime error which has been caused due to logical error and inappropriate delimeter.
Assuming you want to tokenise the big string using newline character, use, \n
as delimeter to StringTokenizer.
Have a look at the corrected code below which satisfies your use case:
import java.util.StringTokenizer;
public class rough {
public static void main(String[] args) throws Exception {
StringTokenizer itr = new StringTokenizer("PKE2324 02-12-2020 200"
+ "\nMJD432 19-05-2019 150", "\n");
while (itr.hasMoreTokens()){
String line = itr.nextToken().toString();
String[] tokens = line.split(" ");
String[] date = tokens[1].split("-");
String yr = date[2];
String reg = tokens[0];
String regy = new String(reg + " " + yr);
System.out.println(regy);
}
}
}
Output:
PKE2324 2020
MJD432 2019
答案2
得分: -1
代码部分不要翻译,以下是您提供的内容翻译后的结果:
异常由这行代码引起
String[] tokens = line.split(" ");
String[] date = tokens[1].split("-");
line = PKE2324,因此 tokens[] = { "PKE2324" } --> 长度 = 1
tokens[1] --> 数组索引越界异常
修复方法:
StringTokenizer itr = new StringTokenizer("PKE2324 02-12-2020 200"
+ "\nMJD432 19-05-2019 150");
默认分隔符是空格。
如果您想要不同的分隔符,应在构造函数中传递分隔符。
英文:
Exception caused by this line
String[] tokens = line.split(" ");
String[] date = tokens[1].split("-");
line = PKE2324, so tokens[] = { "PKE2324" } --> length = 1
tokens[1] --> ArrayIndexOutOfBoundException
To fix this:
StringTokenizer itr = new StringTokenizer("PKE2324 02-12-2020 200"
+ "\nMJD432 19-05-2019 150");
Default delimeter is whitespace.
You should pass delimeter in constructor if you want different delimeter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论