使用给定信息读取文本文件

reading text file with given information

所以我正在尝试将文本文件 drinks.txt 转换为我可以与之交互的 Vending Machine 数组。我得到了以下代码:

import java.io.*;
import java.util.Scanner;

public class VendingMachine {

//data members
private Item[] stock;  //Array of Item objects in machine
private double money;  //Amount of revenue earned by machine

/*********************************************************************
 * This is the constructor of the VendingMachine class that take a
 * file name for the items to be loaded into the vending machine.
 *
 * It creates objects of the Item class from the information in the 
 * file to populate into the stock of the vending machine.  It does
 * this by looping the file to determine the number of items and then
 * reading the items and populating the array of stock. 
 * 
 * @param filename Name of the file containing the items to stock into
 * this instance of the vending machine. 
 * @throws FileNotFoundException If issues reading the file.
 *********************************************************************/
public VendingMachine(String filename) throws FileNotFoundException{
    //Open the file to read with the scanner
    File file = new File(filename);
    Scanner scan = new Scanner(file);

    //Determine the total number of items listed in the file
    int totalItem = 0;
    while (scan.hasNextLine()){
        scan.nextLine();
        totalItem++;
    } //End while another item in file
    //Create the array of stock with the appropriate number of items
    stock = new Item[totalItem];
    scan.close();

    //Open the file again with a new scanner to read the items
    scan = new Scanner(file);
    int itemQuantity = -1;
    double itemPrice = -1;
    String itemDesc = "";
    int count = 0;
    String line = "";

    //Read through the items in the file to get their information
    //Create the item objects and put them into the array of stock
    while(scan.hasNextLine()){
        line = scan.nextLine();
        String[] tokens = line.split(",");
        try {
            itemDesc = tokens[0];
            itemPrice = Double.parseDouble(tokens[1]);
            itemQuantity = Integer.parseInt(tokens[2]);

            stock[count] = new Item(itemDesc, itemPrice, itemQuantity);
            count++;
        } catch (NumberFormatException nfe) {
            System.out.println("Bad item in file " + filename + 
                    " on row " + (count+1) + ".");
        }
    } //End while another item in file
    scan.close();

    //Initialize the money data variable.
    money = 0.0;
} //End VendingMachine constructor

} //End VendingMachine class definition

文本文件如下所示:

Milk,2.00,1 
OJ,2.50,6
Water,1.50,10
Soda,2.25,6
Coffee,1.25,4
Monster,3.00,5

总的来说,我只是想弄清楚如何阅读包含主要方法的 VendingMachineDriver class 下的 drinks.txt。你们对如何做到这一点有什么建议吗?

要声明并创建一个新的文本文件,例如使用名称 "NewTextFile.txt",请执行以下操作:

File newFile=new File("NewTextFile.txt");

这会在您正在工作的项目的当前目录中创建文本文件。如果您只是想在 newFile 对象中分配文件,上述代码也适用目录中已经存在。
希望对你有帮助。

您的文本文件已转换为数组

private Item[] stock;  //Array of Item objects in machine

此处插入元素的位置

stock[count] = new Item(itemDesc, itemPrice, itemQuantity);

您可以像这样对 class 中的 stock 数组做一些事情

for (int i = 0; i < stock.length; i++) {
    Item item = stock[i];
    // Do something with item
}

或者从外部用它做一些事情(你需要为此创建一个 getter)

里面class

public Item getStockForIndex(int i) {
    return stock[i];
}

主要代码在哪里

VendingMachine vm = new VendingMachine("filename.txt");

// Get first item
Item item = vm.getStockForIndex(0);

// Do something with item