简体   繁体   English

为什么我会得到一个 ArrayIndexOutOfBoundsException 呢?

[英]Why do I get an ArrayIndexOutOfBoundsException with this?

Hey there pretty new to Java I have a CSV file that I am scanning line by line (I am assuming) and printing out the details formatted.嘿,Java 很新我有一个 CSV 文件,我正在逐行扫描(我假设)并打印出格式化的详细信息。 I keep getting a java.lang.ArrayIndexOutOfBoundsException: 1 what could I be doing wrong?我不断收到 java.lang.ArrayIndexOutOfBoundsException: 1 我做错了什么? Here is the CSV file contents.这是 CSV 文件内容。

1,Mazda CX-9,7,Automatic,Premium,150

2,VW Golf,5,Automatic,Standard,59

3,Toyota Corolla,5,Automatic,Premium,55

4,VW Tiguan,7,Automatic,Premium,110

5,Ford Falcon,5,Manual,Standard,60
    String fileName = "CarList.CSV";
    File file = new File(fileName);
    Scanner input = new Scanner(file);
    while (input.hasNextLine())
    {
        carsAvailableCount++;
        String line = input.nextLine();
        int lenght = line.length();
        String fields[] = line.split(",");
        String carNo = fields[0];
        String carName = fields[1];
        String seats = fields[2];
        String transmission = fields[3];
        String carType = fields[4];
        String rate = fields[5];

        System.out.format("%-9s%-9s%-9s%-9s%-9s%-9s\n", carNo, carName, seats, transmission, carType, rate);

    }

Figured out the problem haha, my CSV file had empty lines below my last entry.找出问题哈哈,我的 CSV 文件在我最后一个条目下面有空行。 Cheers everyone.大家加油。

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

相关问题 为什么会出现“ ArrayIndexOutOfBoundsException”错误? - Why do I get an 'ArrayIndexOutOfBoundsException' error? 为什么在此素数检查中得到ArrayIndexOutOfBoundsException? - Why do I get an ArrayIndexOutOfBoundsException in this prime number check? 为什么我的JDBC连接中出现ArrayIndexOutOfBoundsException? - Why do I get an ArrayIndexOutOfBoundsException in my JDBC connection? 为什么会收到“ java.lang.ArrayIndexOutOfBoundsException:0” - Why do I get an “java.lang.ArrayIndexOutOfBoundsException: 0” 为什么我在这里得到 java.lang.ArrayIndexOutOfBoundsException ? - Why do I get java.lang.ArrayIndexOutOfBoundsException here? 为什么我得到“ArrayIndexOutOfBoundsException”? - Why did I get an “ArrayIndexOutOfBoundsException”? 为什么我继续获取ArrayIndexOutofBoundsException? - Why do I keep on getting an ArrayIndexOutofBoundsException? 为什么我用此代码不断获取ArrayIndexOutOfBoundsException? - Why do I keep getting an ArrayIndexOutOfBoundsException with this code? 为什么在线程“ AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException:2中得到异常? - Why do I get Exception in thread “AWT-EventQueue-0” java.lang.ArrayIndexOutOfBoundsException: 2? 尝试在Realm中删除对象时,为什么会出现ArrayIndexOutOfBoundsException? - Why I get ArrayIndexOutOfBoundsException when try delete objects in Realm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM