从CSV文件自动创建数组时,索引1超出了长度1错误的范围 [英] Index 1 out of bounds for length 1 error while creating the array automatically from the CSV file

查看:578
本文介绍了从CSV文件自动创建数组时,索引1超出了长度1错误的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题看似微不足道,但我仍在努力。错误是 java.lang.ArrayIndexOutOfBoundsException:索引1的长度为1 ,据我了解,只能访问数组中的第一个索引( id (在这种情况下是主键)。这是为什么?加载文件CSV文件时,我是否不根据定界符分隔的项目数自动确定数组的长度?然后我的 readFile()方法怎么了?

The problem seems to be trivial but I still struggle on it. The error is java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 from which I understand only the first index from the array can be accessed (id which is the primary key in that case). Why is that? Don't I determine the length of the array automatically based on the number of items separated by delimiter when I load the file CSV file ? What is wrong with my readFile() method then ?

import javax.persistence.*;

@Table
@Entity(name="users")

public class UserData {



    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column
    private int telephoneNo;

    @Column
    private String email;

    @Column
    private String pickUp;

    @Column
    private String dropOff;

    public UserData(){}


    public UserData(String firstName, String lastName, int telephoneNo, String email, String pickUp, String dropOff){
        this.firstName = firstName;
        this.lastName = lastName;
        this.telephoneNo = telephoneNo;
        this.email = email;
        this.pickUp = pickUp;
        this.dropOff = dropOff;
    }



    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public int getTelephoneNo() {
        return telephoneNo;
    }

    public void setTelephoneNo(int telephoneNo) {
        this.telephoneNo = telephoneNo;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPickUp() {
        return pickUp;
    }

    public void setPickUp(String pickUp) {
        this.pickUp = pickUp;
    }

    public String getDropOff() {
        return dropOff;
    }

    public void setDropOff(String dropOff) {
        this.dropOff = dropOff;
    }
}



sample.csv



sample.csv

1,Reanna,Colepio,159031625,reanna_colepio123@gmail.com,London,Glasgow
2,Rita,Das,987443767,ritadas@outlook.com,Edinburgh,Glasgow



ManageData.java



ManageData.java

import org.hibernate.Session;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.util.Scanner;

public class ManageData {

    public static void readFile() {
        String line = "";

        Session session = HibernateConfig.getSessionFactory().openSession();

        try (BufferedReader br = new BufferedReader(new FileReader("/Users/ggabrychowicz/IdeaProjects/Bus Managing System/src/main/java/sample.csv"))){

            while ((line = br.readLine()) != null){
                String[] tempArr = line.split(",");

                UserData userData = new UserData();


                userData.setFirstName(tempArr[1]);
                userData.setLastName(tempArr[2]);
                userData.setTelephoneNo(Integer.parseInt(tempArr[3]));
                userData.setEmail(tempArr[4]);
                userData.setPickUp(tempArr[5]);
                userData.setDropOff(tempArr[6]);

                session.beginTransaction();
                session.save(userData);
                session.getTransaction().commit();

            }
            session.close();
        }

        catch (IOException e) {
            if (session!=null){
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
    }
}

这就是我的意思当我打印数组时得到:

1
Reanna
Colepio
159031625
reanna_colepio123@gmail.com
London
Glasgow
2
Rita
Das
987443767
ritadas@outlook.com
Edinburgh
Glasgow
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at ManageData.readFile(ManageData.java:26)
    at Main.main(Main.java:4)


推荐答案

我对csv和room不太了解,但是我读过某处的标头也算作一行

I don't know much about csv and room , but i read somewhere the header is also counted as a row

所以

我认为当您说 line = br.readLine()时,它会读取csv文件的头文件进行第一次迭代,并且该头文件没有任何逗号,因此 line.split 仅返回ome字符串,当您说 temArr [1] 时,这是错误的因为数组中只有一个字符串,并且索引为零。

i think when you say line = br.readLine() it read the header of the csv file for the first iteration, and that header does not have any comma so line.split only returns ome string , and when you say temArr[1] it is wrong because there is only one string in the array and it is at zero index.

您应该尝试打印该数组,否则您会知道天气是否正确,请还请让我知道,因为我不确定,这只是个主意。

You should try printing that array then you will know weather or not i am right, and please also let me know, cuz it is just an idea, i am not sure.

如果事实是这样,您可以 br.readline 进入while循环之前,它将按预期运行

And if that turns out to be the case you can you br.readline before entering the while loop and it will work an you expected

这篇关于从CSV文件自动创建数组时,索引1超出了长度1错误的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆