我必須導入包含單詞和整數的文本文件,然後使用二維數組維護數據。我似乎無法弄清楚如何製作這個數組。我只能使用Scanner,File和FileNotFoundException。這裏是我的代碼的片段:在java中爲文本文件創建一個二維數組?
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的新手,這是一項任務。我很感激你能給予的幫助。
您能告訴我們輸入'records.txt'文件的結構嗎? –
是的,它是一個14行7列的文本文件,具有不同類型的食物,屬性和定價。 – Paul
@Paul將文本文件的實際內容作爲您的問題的一部分包含在內。 – Gosu