【发布时间】:2016-03-18 06:30:42
【问题描述】:
我正在为一个应该读取文本文件并从所述文本文件中获取项目以填充项目对象数组的类创建一个自动售货机程序。然而,当我去打印数组时,这就是 JVM 发出的结果。
物品@3d4eac69
我已经调试了程序,看看文件是否被正确读取,值是否被正确输入,并且确实如此。我已经尝试了几乎所有我能想到的东西,作为一个初学者,我不知道该怎么做。我将在下面包含我的代码。 VendingMachine 构造函数由讲师提供给我们。
自动售货机.java 导入 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
private Item[] vendor;
/*********************************************************************
* 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
//To run the successful transaction
public void vend() {
}
//To determine whether or not the transaction was successful
public void outputMessage() {
}
//To print the items in held in stock
public void printMenu() {
vendor = stock;
System.out.println(this.vendor[0]);
}
} //End VendingMachine class definition
VendingMachineDriver.java
import java.util.*;
import java.io.*;
public class VendingMachineDriver {
public static void main(String args[]) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
//String vendingSelect = input.next();
String a = new String("a");
String b = new String("b");
String x = new String("x");
System.out.println("Welcome to Jeremy's Super Vending Machines!");
System.out.println("Please enter how much money you have:");
System.out.println("Press A to select Drinks");
//String vendingSelect = input.next();
VendingMachine drinks = new VendingMachine("vending_machines/drinks");
drinks.printMenu();
}
}
Item.java(我们只被告知在这个文件中有数据成员,老实说我不知道为什么)
import java.util.*;
public class Item {
private String itemDesc;
private double itemPrice;
private int itemQuantity;
public Item (String itemDesc, double itemPrice, int itemQuantity){
this.itemDesc = itemDesc;
this.itemPrice = itemPrice;
this.itemQuantity = itemQuantity;
}
}
编辑:忘记添加文本文件和相应的位置
饮料(位置是 vending_machines/drinks)
Milk,2.00,1
OJ,2.50,6
Water,1.50,10
Soda,2.25,6
Coffee,1.25,4
Monster,3.00,5
零食(位置是 vending_machines/snacks)
Gummies,1.50,6
Chips,1.00,6
Raisins,1.25,5
Pretzels,1.50,6
Cookie,1.75,5
Peanut,1.25,4
Gum,0.75,2
【问题讨论】:
-
如果您希望它为您打印“有用”信息,则需要覆盖
toStringinitem,如果您直接打印对象。