我正在为一个类创建一个自动售货机程序,该程序应该读取文本文件并从所述文本文件中获取项目来填充项目对象数组。然而,当我去打印数组时,这就是 JVM 打印出来的内容。
Item@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
最佳答案
添加/重写 Items 类的 toString 方法
@Override
public String toString() {
return "c1 [itemDesc=" + itemDesc + ", itemPrice=" + itemPrice + ", itemQuantity=" + itemQuantity + "]";
}
否则 java 会打印对象的哈希码,而不是人类可读的信息...
关于java - 自动售货机程序及打印阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36077616/