如何从文件中填充对象数组

How to fill in an array of objects from a file

在这里,我尝试使用 BufferedReader 来填充我的对象数组 问题是我收到一条错误消息,指出数组在 1

处超出索引

此外,文件中的每个标记都由一个 space 分隔,因此我可以将标记拆分为字符串数组并将其划分为属性

文件内容

Introduction_to_Algorithms Thomas_H.Cormen 2011 2 18
Code_Complete Steve_McConnell 1946 2 18
Design_Patterns Erich_Gamma 2004 2 18
The_Pragmatic_Programmer Andrew_Hunt 2021 2 18
Head_First_Design_Patterns Eric_Freeman 1937 2 18
Refactoring Martin_Fowler 2003 2 18
Clean_Code Robert_C.Martin 2008 2 18
BRIGHT_STAR Yuyi_Morales 1867 2 6
DREAM_STREET Tricia_Elam_Walker 1927 2 5
THE_LONGEST_STORM Dan_Yaccarino 1943 2 5
NICKY_&_VERA Peter_Sís 1781 2 4
UNSPEAKABLE Carole_Boston 1677 2 6
WATERCRESS Andrea_Wang 1884 2 12
WE_ALL_PLAY Julie_Flett 1925 2 15

(( 姓名,作者,已发布,numCopies,properAge,science ))


import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class BooksReader {

    static book arbook[] = new book[14];

    public static void array_filler() throws FileNotFoundException {

        String Line = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader("MyPath"));

            while ((Line = br.readLine()) != null) {

                int i = 0;

                String ar[] = Line.split(" ");
//The attributes of each object are two strings and 3 integers
               arbook[i] = new book(ar[0], ar[1], Integer.parseInt(ar[2]),Integer.parseInt(ar[3]), Integer.parseInt(ar[4])); //here is the error 
                i++;
               
            }

        } catch (IOException ex) {
            Logger.getLogger(BooksReader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        BooksReader.array_filler();
    }
}
public class Book {

    private String bookName;
    private String bookAuthor;
    private String SerialNumber;
    private int numCopies;
    private int properAge;
    private int bookReleaseDate;
/// here is the non default constructor of class named book 
    public Book(String bookName, String bookAuthor, int bookReleaseDate, int numCopies, int properAge) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.numCopies = numCopies;
        this.properAge = properAge;
        this.bookReleaseDate = bookReleaseDate;
    }

    public String getBookName() {
        return bookName;
    }

    public int getNumCopies() {
        return numCopies;
    }

    public int getProperage() {
        return properAge;

    }

    public void setBookName(String BookName) {
        this.bookName = BookName;
    }

    public void setBookAuthor(String BookAuthor) {
        this.bookAuthor = BookAuthor;
    }

    public void setReleaseDate(int bookReleaseDate) {
        this.bookReleaseDate = bookReleaseDate;
    }

    public void setNumCopies(int numCopies) {

        this.numCopies = numCopies;

    }

    public void setProperAge(int ProperAge) {

        this.properAge = ProperAge;

    }

    public void setSerialNumber(String SerialNumber) {
        this.SerialNumber = SerialNumber;

    }

    public boolean checkAge(int ageOfUser) {
        if (ageOfUser >= properAge) {
            return true;
        }
        return false;
    }

    public int addNumCopies() {
        this.numCopies += 1;
        return numCopies;
    }

    public int decNumCopies() {
        this.numCopies -= 1;
        return numCopies;
    }

}

那么有人可以帮助我编写代码吗? 甚至建议另一种方法?

您需要将 int i = 0; 移动几行到 while 循环之前,因为它现在的工作方式,您将始终尝试访问 arbook[0]

嗯,填充对象数组 首先,我必须通过 for 循环传递整个数组,将其值设置为默认值(例如整数为 0,字符串为空字符串),稍后我将从文件

覆盖