Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.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读取.csv文件并获取Java.lang.ArrayIndexOutOfBoundsException:3和don';我不知道为什么_Java_Arrays_Csv_Indexoutofboundsexception - Fatal编程技术网

Java读取.csv文件并获取Java.lang.ArrayIndexOutOfBoundsException:3和don';我不知道为什么

Java读取.csv文件并获取Java.lang.ArrayIndexOutOfBoundsException:3和don';我不知道为什么,java,arrays,csv,indexoutofboundsexception,Java,Arrays,Csv,Indexoutofboundsexception,我正为我要完成的一个项目而头痛不已。我相信答案很简单,但我就是想不出解决办法 当我写入csv文件时,它可以工作,但当从中读取时,如果csv文件中有3个以上的对象,我会得到ArrayIndex错误,但只有3个或更少,并且不会抛出错误 下面是我写入文件的代码: void saveDataToFile(){ String op=”“; 试一试{ FileWriter fw=新的FileWriter(文件名); for(int i=0;i

我正为我要完成的一个项目而头痛不已。我相信答案很简单,但我就是想不出解决办法

当我写入csv文件时,它可以工作,但当从中读取时,如果csv文件中有3个以上的对象,我会得到ArrayIndex错误,但只有3个或更少,并且不会抛出错误

下面是我写入文件的代码:

void saveDataToFile(){

String op=”“;
试一试{
FileWriter fw=新的FileWriter(文件名);
for(int i=0;i
下面是读取该文件的代码:

void loadDataFromFile(){

试试看{
文件fi=新文件(文件名);
FileReader fr=新的FileReader(fi);
char[]buffer=新字符[(int)fi.length()];
fr.read(缓冲区);
fr.close();
字符串全部=新字符串(缓冲区);
字符串[]ip=all.split(“\n”);

对于(int i=0;i我猜这个循环正在破坏它:

       for(int j=0; j<op.length; j++){
            if(make.equals("Ford"))
                cars[i] = new Ford(model,year);
            else if(make.equals("Mazda"))
                cars[i] = new Mazda(model,year,colour);
            else if(make.equals("Toyota"))
                cars[i] = new Toyota(model,year,h);

        }

用于(int j=0;jj哪一行引发异常?您是否获得了包含错误的堆栈跟踪?查看它引发异常的位置会很有帮助。错误:java.lang.ArrayIndexOutOfBoundsException:3这是我得到的全部信息,因此我无法确切地看到它来自何处。Perception,我不太理解您的评论。抱歉,这是故意的o be cars[j]不是我。我已经更改了,但没有任何效果。在catch子句中添加一个e.printStackTrace();应该可以很快缩小范围。非常感谢Daniel。进行了堆栈跟踪,并且能够修复错误。非常感谢您的帮助!不确定如何给您代表,但一旦我找到答案,我会给您发送一些。
    try{
        File fi = new File(filename);
        FileReader fr = new FileReader(fi);
        char[] buffer = new char[(int)fi.length()];
        fr.read(buffer);
        fr.close();

        String all = new String(buffer);
        String[] ip = all.split("\n");

        for (int i=0; i<ip.length; i++){ 
            String[] op = ip[i].split(",");

            String author = op[0];
            String title = op[1];
            int isbn = Integer.parseInt(op[2]);
            String s = op[3];
            boolean h = Boolean.parseBoolean(op[3]);

            for(int j=0; j<op.length; j++){
                if(author.equals("Dickens"))
                    library[i] = new title(author,isbn);
                else if(author.equals("Lumas"))
                    library[i] = new title(author,isbn,s);
                else if(author.equals("Orwell"))
                    library[i] = new title(author,isbn,h);

            }
        }

    }
    catch(Exception e) {
        System.out.println("ERROR : "+e);
    }
            System.out.println("loadDataFromFile()");
    }
       for(int j=0; j<op.length; j++){
            if(make.equals("Ford"))
                cars[i] = new Ford(model,year);
            else if(make.equals("Mazda"))
                cars[i] = new Mazda(model,year,colour);
            else if(make.equals("Toyota"))
                cars[i] = new Toyota(model,year,h);

        }