Index 1 out of bounds for length 1 error while creating the array automatically from the CSV file

元气小坏坏 提交于 2020-04-18 03:49:17

问题


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 ?


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();
        }
    }
}

That's what I get when I print the array:

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)

回答1:


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

So

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.

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



来源:https://stackoverflow.com/questions/60865319/index-1-out-of-bounds-for-length-1-error-while-creating-the-array-automatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!