Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java数组索引越界_Java - Fatal编程技术网

Java数组索引越界

Java数组索引越界,java,Java,我有下面的代码,它通读一行学生,程序应该在每个空格处拆分,然后转到文本的下一部分,但我得到arrayindexoutofBound异常。 文本文件有以下几行: 130002 Bob B2123 35 34 B2132 34 54 B2143 23 34 public static void main(String[] args) throws FileNotFoundException { File f = new File("C:\\User

我有下面的代码,它通读一行学生,程序应该在每个空格处拆分,然后转到文本的下一部分,但我得到arrayindexoutofBound异常。 文本文件有以下几行:

130002 Bob    B2123   35   34   B2132   34   54   B2143   23   34



public static void main(String[] args) throws FileNotFoundException {
    File f = new File("C:\\Users\\Softey\\Documents\\scores.txt");
    Scanner sc = new Scanner(f);

    List<MarkProcessing> people = new ArrayList<MarkProcessing>();

    while(sc.hasNextLine()){
        String line = sc.nextLine();
        String[] details = line.split("\\s+");

        String regNumber = details[0];
        String name = details[1];
        String modOne= details[2];
        int courseM = Integer.parseInt(details[3]);
        int examM = Integer.parseInt(details[4]);
        String modTwo = details[5];
        int courseM2 = Integer.parseInt(details[6]);
        int examM2 = Integer.parseInt(details[7]);
        String modThree = details[8];
        int courseM3 = Integer.parseInt(details[9]);
        int examM3= Integer.parseInt(details[10]);

        MarkProcessing p = new MarkProcessing(regNumber, name, modOne,courseM, examM, modTwo,courseM2,examM2, modThree, courseM3,  examM3);
        people.add(p);
    }


}
130002鲍勃B2123 35 34 B2132 34 54 B2143 23 34
公共静态void main(字符串[]args)引发FileNotFoundException{
文件f=新文件(“C:\\Users\\Softey\\Documents\\scores.txt”);
扫描仪sc=新扫描仪(f);
List people=new ArrayList();
while(sc.hasNextLine()){
字符串行=sc.nextLine();
String[]details=line.split(\\s+);
字符串regNumber=详细信息[0];
字符串名称=详细信息[1];
字符串modOne=details[2];
int courseM=Integer.parseInt(详细信息[3]);
int examM=Integer.parseInt(详细信息[4]);
字符串modTwo=详细信息[5];
int courseM2=Integer.parseInt(详细信息[6]);
int examM2=Integer.parseInt(详细信息[7]);
字符串modThree=详细信息[8];
int courseM3=Integer.parseInt(详细信息[9]);
int examM3=Integer.parseInt(详细信息[10]);
MarkProcessing p=新的MarkProcessing(regNumber、name、modOne、courseM、examM、modTwo、courseM2、examM2、modThree、courseM3、examM3);
新增(p);
}
}
}


当转到详细信息[1]时,我得到索引错误。

要按空格分割,需要使用:

String[] details = line.split(" "); // "\\s+" -> does not split by space.

在您的例子中,它试图用正则表达式模式“//s+”拆分该行,由于找不到该行,因此它将整行视为一个字符串。在本例中,字符串数组的大小为1。没有详细信息[1],因此您会出现此错误。

如果没有关于输入文件的信息,我猜这是因为您的文件中有空行。如果是这种情况,你应该尝试一些方法来确保你有足够的碎片。。为此,您的while循环可以是这样的

while(sc.hasNextLine()){
    String line = sc.nextLine();
    String[] details = line.split("\\s+");
    if(details.length < 11) continue; // skip this iteration
    ...
}
while(sc.hasNextLine()){
字符串行=sc.nextLine();
String[]details=line.split(\\s+);
如果(details.length<11)继续;//跳过此迭代
...
}

请记住,只有在每行检查至少11个项目时,这才有效。如果您需要更高级的输入解析方法,而他们可能有任意数量的课程。您最好考虑另一种方法,而不是直接从索引中存储值。

您应该在解析该行之前尝试打印该行,以便查看是什么原因导致它爆炸

    String line = sc.nextLine();
    String[] details = line.split("\\s+");

    String regNumber = details[0];
    String name = details[1];
    String modOne= details[2];
您正在对大块的空格进行拆分。如果遇到没有空格的行,则只有一个元素,因此
details[1]
将抛出
IndexOutOfBoundsException

我的建议是仔细检查你的意见。它在尾线馈送时有吗?如果是这样,则可能会被解释为一个空行

130002 Bob    B2123   35   34   B2132   34   54   B2143   23   34
<blank line>
130002鲍勃B2123 35 34 B2132 34 54 B2143 23 34

哪一行导致异常?让它检查分割数组的大小,如果小于11,则打印出弄乱的行。可能添加一个文件和整个错误消息?可能是因为文件中有空行。尝试添加一个条件,如,
if(details.length)。您确实应该在此处添加大量异常处理,以确保获得预期的值。\\s+拆分任意数量的空格。这不是原因。请参阅此处的
\\s+
正则表达式:=>它匹配任意数量的空格。。。