Java将数据文件读入多维数组

发布于 2024-10-14 19:25:59 字数 2070 浏览 8 评论 0原文

我有一个 .dat 文件,我想将其加载到自定义数组中。我如何让它实际将数据加载到数组中。数据由 (String, int, int, double, String) 组成。

class CDinventoryItem{


   private CDinventoryItem [] inven = new CDinventoryItem[1000];


    public CDinventoryItem (String title, int itemNumber, int numberofUnits, 
    double unitPrice, String genre){

              DataInputStream input;

            try{

                input = new DataInputStream(new FileInputStream("inventory.dat"));

                inven = input.read(CDinventoryItem[]);  //line I am receiving error on


            }
            catch ( IOException error ){
                JOptionPane.showMessageDialog( null, "File not found",
                "" ,JOptionPane.ERROR_MESSAGE);
            }


        }

}

所以现在 readFile 在它自己的类中......

class readFile {
public CDinventoryItem[] inven;

    public readFile(){

BufferedReader in = null;

try {
    in = new BufferedReader(new FileReader("inventory.dat"));
    String line = null;
    int i = 0;
    while ((line = in.readLine()) != null) {

        // process each line
        String[] parts = line.split(",");
        String title = parts[0];
        int itemNumber = Integer.parseInt(parts[1]);
        int numberofUnits = Integer.parseInt(parts[2]);
        double unitPrice = Double.parseDouble(parts[3]);
        String genre = parts[4];

        CDinventoryItem item = new CDinventoryItem(title, itemNumber, numberofUnits, 
unitPrice, genre);

        //add item to array
        inven[i] = item;
        i++;
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}}

我从我的 CDinventory 类中调用它

        readFile invenItem = new readFile();
        list = new JList(invenItem.inven);

,但它给了我一个: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 在线的: readFile invenItem = new readFile();

似乎不喜欢我那样传递数组。

I have a .dat file that I want to load into a custom array. How do I get it to actually load the data into the array. The data consists of a (String, int, int, double, String).

class CDinventoryItem{


   private CDinventoryItem [] inven = new CDinventoryItem[1000];


    public CDinventoryItem (String title, int itemNumber, int numberofUnits, 
    double unitPrice, String genre){

              DataInputStream input;

            try{

                input = new DataInputStream(new FileInputStream("inventory.dat"));

                inven = input.read(CDinventoryItem[]);  //line I am receiving error on


            }
            catch ( IOException error ){
                JOptionPane.showMessageDialog( null, "File not found",
                "" ,JOptionPane.ERROR_MESSAGE);
            }


        }

}

So now readFile is in its own class...

class readFile {
public CDinventoryItem[] inven;

    public readFile(){

BufferedReader in = null;

try {
    in = new BufferedReader(new FileReader("inventory.dat"));
    String line = null;
    int i = 0;
    while ((line = in.readLine()) != null) {

        // process each line
        String[] parts = line.split(",");
        String title = parts[0];
        int itemNumber = Integer.parseInt(parts[1]);
        int numberofUnits = Integer.parseInt(parts[2]);
        double unitPrice = Double.parseDouble(parts[3]);
        String genre = parts[4];

        CDinventoryItem item = new CDinventoryItem(title, itemNumber, numberofUnits, 
unitPrice, genre);

        //add item to array
        inven[i] = item;
        i++;
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}}

and I am calling it from my CDinventory class

        readFile invenItem = new readFile();
        list = new JList(invenItem.inven);

but it gives me a: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
on line:
readFile invenItem = new readFile();

Doesn't seem to like me passing the array that way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2024-10-21 19:25:59

您需要逐行读取文件。拆分 , 上的每一行并创建一个 CDInventoryItem。将项目添加到您的数组中。

另请注意,此方法不应位于 CDInventoryItem 的构造函数中。您的 CDInventoryItem 类甚至不应该有一个 CDInventoryItem 数组。所有这些都应该在一个单独的类中完成。

下面是一些可以帮助您入门的代码:

public void readFile() {

    BufferedReader in = null;

    try {
        in = new BufferedReader(new FileReader("inventory.dat"));
        String line = null;
        int i = 0;
        while ((line = in.readLine()) != null) {

            // process each line
            String[] parts = line.split(",");
            String title = parts[0];
            int itemNumber = Integer.parseInt(parts[1]);
            int numberOfUnits = Integer.parseInt(parts[2]);
            double unitPrice = Double.parseDouble(parts[3]);
            String genre = parts[4];

            CDinventoryItem item = new CDinventoryItem(title, itemNumber, unitPrice, genre);

            //add item to array
            inven[i] = item;
            i++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

You need to read the file line by line. Split each line on , and create a single CDInventoryItem. Add the item to your array.

Also, note that this method should not be in the constructor of CDInventoryItem. Your CDInventoryItem class should not even have an array of CDInventoryItems. All this should be done in a separate class.

Here is some code to get you started:

public void readFile() {

    BufferedReader in = null;

    try {
        in = new BufferedReader(new FileReader("inventory.dat"));
        String line = null;
        int i = 0;
        while ((line = in.readLine()) != null) {

            // process each line
            String[] parts = line.split(",");
            String title = parts[0];
            int itemNumber = Integer.parseInt(parts[1]);
            int numberOfUnits = Integer.parseInt(parts[2]);
            double unitPrice = Double.parseDouble(parts[3]);
            String genre = parts[4];

            CDinventoryItem item = new CDinventoryItem(title, itemNumber, unitPrice, genre);

            //add item to array
            inven[i] = item;
            i++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文