簡體   English   中英

在Java中為文本文件創建二維數組?

[英]Creating a two dimensional array for a text file in java?

我必須導入同時包含單詞和整數的文本文件,然后使用二維數組維護數據。 我似乎無法弄清楚如何制作此數組。 我只能使用Scanner,File和FileNotFoundException。 這是我的代碼片段:

public static void DisplayInventory() throws FileNotFoundException
{

    try
    {
        Scanner autoInventory = new Scanner(new File("records.txt"));

        for (int Num = 0; Num < 15; Num++)
        {
            String autoRecords = autoInventory.nextLine();
            autoRecords = autoRecords.replace(';', ' ');
            System.out.println(autoRecords);

        }

    }
    catch (FileNotFoundException except)
    {
        System.out.println("Error: Inventory read failure. Error " +
            except.getMessage());
        System.exit(-1);

    }

}

如您所見,它沒有顯示數組。 我不確定該怎么做,現在已經待了幾天。 我是Java的新手,這是一項作業。 感謝您提供的任何幫助。

我將作一些假設(可能不准確)。 首先是您的文件如下所示(一個具有14行7列的csv):

a1,b1,c1,d1,e1,f1,g1
a2,b2,c2,d2,e2,f2,g2
a3,b3,c3,d3,e3,f3,g3
a4,b4,c4,d4,e4,f4,g4
..

下一個假設是您的文件包含String類型的值。 在執行任何操作之前,我們需要聲明2D數組:

int numRecords = 14;
int numColumns = 7;
String[][] records = new String[numRecords][numColumns];

您發布的代碼嘗試從掃描儀讀取15行,對進行替換; (不確定全部內容),然后打印該行。 這是一個好的開始-此循環負責讀取每一行。 我還將添加一個附加條件,以檢查是否確實存在下一行(使用hasNextLine )。

for (int i = 0; i < numRecords && autoInventory.hasNextLine(); i++)
{
    String rowData = autoInventory.nextLine();
    String[] colData = rowData.split(",");

    for (int j=0; j<colData.length && j<numColumns; j++)
    {
        records[i][j] = colData[j];
    }
}

在這個外部循環中,我們首先使用nextLine收集rowData 接下來,我們使用split (String上的一個函數)將線拆分為多個部分(以,分隔),並將這些部分存儲在一維數組中。 我們聲明第二個循環(內部循環),循環遍歷colData對於我們檢查的每個值,我們將其放置在records數組中的適當位置。

現在文件已被讀入數組,您可以用類似的方式將其打印到屏幕上。 使用外部循環(i)遍歷行,使用內部循環(j)遍歷列,打印出每個records[i][j]

for (int i=0; i<numRecords; i++) 
{
    for (int j=0; j<numColumns; j++) 
    {
        System.out.print(records[i][j]);

                                    // extra stuff..
        if (j != numColumns-1) {    // print commas between items if we want
            System.out.print(", ");
        }
    }
    System.out.println("");  // print out a newline after each row
}

我不完全了解您要做什么或為什么需要二維數組,但是這里是如何創建數組和二維數組,以及如何使用for向其添加數據的方法。環。 但是,我認為您不需要二維數組。

public static void DisplayInventory() throws FileNotFoundException
{
String[] sArray = new String[15]; //I'm assuming there are 15 values being added
String[][] sArray2 = new String[10][10]; //Not sure how many are being added
//Remember, make sure you don't use less space than you need.

    try
    {
        Scanner autoInventory = new Scanner(new File("records.txt"));

        for (int Num = 0; Num < 15; Num++)
        {
            String autoRecords = autoInventory.nextLine();
            autoRecords = autoRecords.replace(';', ' ');
            sArray[Num] = autoRecords;
            System.out.println(sArray[Num]);
        }

我認為這樣就足夠了,因為我不認為您需要一個二維數組。 看來您要執行的操作可以通過常規數組進行管理。 話雖如此,但我仍然不完全了解您要執行的操作,因此,如果您確實需要使用二維數組,只需添加另一個for循環,並且在使用該數組時,您可能會 (必要)需要將第一個for循環的值保留在第一個括號內,並將嵌套在第二個括號內-像這樣:

sArray2[Num][Num2];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM