简体   繁体   English

“ java.lang.ArrayIndexOutOfBoundsException:1”运行时错误

[英]“java.lang.ArrayIndexOutOfBoundsException: 1” Error When Run

The program compiles fine, but when I run it, I get an error, more specifically this: java.lang.ArrayIndexOutOfBoundsException : 1 该程序可以正常编译,但是运行该程序时会出现错误,更具体地说是: java.lang.ArrayIndexOutOfBoundsException :1

It is getting the error on: 出现错误:

String name = array[1];

I am not sure why. 我不知道为什么。

This is the code with the problem: 这是有问题的代码:

        infile = new Scanner(new FileReader("EmployeeData.TXT"));
        while(infile.hasNext()){
            String line = infile.nextLine();
            String array[] = line.split(":");
            String name = array[1];
            String id = array[2];
            double salary = Double.parseDouble(array[3]);
            Employee e;
            if (array[0].equals("s")){
                e = new SalariedWorker(id, name, salary);}
            else {
                boolean overtime = Boolean.parseBoolean(array[4]);
                if(overtime){
                    int maxhu = Integer.parseInt(array[5]);
                    e = new HourlyWorker(id, name, salary, maxhu);
                }
                else{
                    e = new HourlyWorker(id, name , salary);
                }
            }
            company.add(e);
        }

For reference, this is the rest of the program: 作为参考,这是程序的其余部分:

It reads this text file called "EmployeeData.TXT": 它读取名为“ EmployeeData.TXT”的文本文件:

S       Washington,George       000001      125000
H   MacDonald,Ronald        386218     7.80 true  40
H       Walton,Samuel           268517  8.21    false
H   Thomas,David            131313  9.45    true    38
H   Sanders,HarlandDavid    277651  8.72    false
S   Baron,James         368535  310236

You're splitting on ":" , and your text file: 您正在分割":"和文本文件:

S       Washington,George       000001      125000
H   MacDonald,Ronald        386218     7.80 true  40
H       Walton,Samuel           268517  8.21    false
H   Thomas,David            131313  9.45    true    38
H   Sanders,HarlandDavid    277651  8.72    false
S   Baron,James         368535  310236

does not have any such character. 没有任何此类字符。 So each array will have size 1. 因此,每个数组的大小为1。

It makes no sense to split on a non-existing character, which makes me very curious why you would choose to use ":" . 分割不存在的字符没有任何意义,这让我很好奇为什么您会选择使用":" Myself, I'd split on white space, "\\\\s+" 我自己,我将空格分隔为"\\\\s+"

As an aside, this problem is very amenable to debugging either with a debugger or with some printlns: 顺便说一句,此问题非常适合使用调试器或某些printlns进行调试:

while(infile.hasNext()){
    String line = infile.nextLine();
    System.out.println("Unsplit String: " + line);
    String array[] = line.split(":");
    System.out.println("Split String: " + java.util.Arrays.toString(array));

Either way, in the future when you have similar problems, find out just what your variables are holding at the time and place of the error, and that often will lead you to the origin of the problem and thereby to its solution. 无论哪种方式,将来在您遇到类似的问题时,只需找出变量在错误发生的时间和地点所持有的内容,这通常会导致您找到问题的根源并进而解决问题。

It is breaking on these lines: 它打破了这些行:

    String line = infile.nextLine();
    String array[] = line.split(":");
    String name = array[1];
    String id = array[2];

You are trying to assign the value of array[1] to name... But the array doesn't contain enough elements. 您正在尝试将array [1]的值分配给name ...但是该数组包含的元素不足。

So say you have line = "hey" and you call split on line, your array will only be of length 1, since there is no ":" character to split on. 因此,假设您有line = "hey"并在行上调用split,则数组将只有1个长度,因为没有要分割的“:”字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM