hibernate 从CSV文件自动创建数组时,索引%1超出长度%1的界限时出错

5hcedyr0  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(110)

这个问题看似微不足道,但我仍在努力解决。错误是java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1,据我所知,只能访问数组中的第一个索引(在这种情况下,id是主键)。为什么会这样呢?当我加载文件CSV文件时,不是可以根据由分隔符分隔的项数自动确定数组的长度吗?那么我的readFile()方法有什么问题呢?

UserData.java

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

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

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)
qvtsj1bj

qvtsj1bj1#

我不太了解CSV和房间,但我在哪里读到标题也被算作一行
所以
我认为当您说line = br.readLine()时,它会读取第一次迭代的CSV文件的头文件,而头文件没有任何逗号,所以line.split只返回一些字符串,而当您说temArr[1]时,它是错误的,因为数组中只有一个字符串,而且它的索引值为零。
你应该试着打印那个数组,然后你就会知道我是不是对的,也请让我知道,因为这只是一个想法,我不确定。
如果事实证明是这样,那么您可以在进入While循环之前执行br.readline,它将按照您预期的那样工作

相关问题