这个问题看似微不足道,但我仍然在努力解决。错误是 java.lang.ArrayIndexOutOfBoundsException: Index 1 out ofbounds 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;
}
}
样本.csv
1,Reanna,Colepio,159031625,reanna_colepio123@gmail.com,London,Glasgow
2,Rita,Das,987443767,ritadas@outlook.com,Edinburgh,Glasgow
管理数据.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 不太了解,但我在某处读到标题也算作一行
所以
我认为当你说 line = br.readLine()
时,它会读取第一次迭代的 csv 文件的标题,并且该标题没有任何逗号,因此 line.split
只返回 ome string ,当你说 temArr[1]
时,这是错误的,因为数组中只有一个字符串,并且它的索引为零。
你应该尝试打印该数组,然后你就会知道天气是否正确,也请让我知道,因为这只是一个想法,我不确定。
如果事实证明是这种情况,您可以在进入 while 循环之前 br.readline
,它将按照您的预期工作
关于java - 从 CSV 文件自动创建数组时,索引 1 超出长度 1 的范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60865319/