阅读给定信息的文本文件

时间:2016-03-28 06:31:50

标签: java oop java-io

所以我试图将文本文件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

总的来说,我只想弄清楚在包含main方法的VendingMachineDriver类下读取drinks.txt。你们有什么建议怎么做?

2 个答案:

答案 0 :(得分:0)

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

<button value="all" type="button" class="color btn btn-lg btn-default">All</button>
<button value="green" type="button" class="color btn btn-lg btn-success">Green</button>
<button value="blue" type="button" class="color btn btn-lg btn-info">Blue</button>

这将在您正在使用的项目的当前目录中创建文本文件。如果您只想在已存在的 newFile 对象中分配文件,则上述代码也可以使用。目录。
希望这会对你有所帮助。

答案 1 :(得分:0)

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

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

这里插入元素

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

您可以使用类中的stock数组执行此操作

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

或者从外面做一些事情(你需要为此创建一个吸气剂)

在课堂内

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