字符串标记化器未读取数据

huangapple go评论74阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月17日 16:18:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64400402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定