我有一个文件Inventory.txt,我正在尝试打印到屏幕上。文件中的每一行都进入一个对象数组。我的代码编译没有错误,但是当我运行它时,没有任何东西打印到屏幕上。我使用Mac和TextEdit /终端。
import java.util.Scanner;
import java.io.*;
public class VendingMachineSimulator
{
to be imported
public static void main(String[] args) throws FileNotFoundException
{
File InventoryFile = new File("Inventory.txt");
Scanner input = new Scanner(InventoryFile);
//This code block will count the number of lines(products) are in the text file
int counter = 0;
while (input.hasNextLine())
{
counter = counter + 1;
}
Inventory[] InventoryObject = new Inventory[counter];
String line = "";
for (int i = 0; i < counter; i++)
{
String[] ProductArray = line.split("-");
InventoryObject[i] = new Inventory(Integer.valueOf(ProductArray[0]), ProductArray[1], ProductArray[2],
ProductArray[3],Double.valueOf(ProductArray[4]), ProductArray[5],
Integer.valueOf(ProductArray[6]));
}
for (int i = 0; i < counter; i++)
{
System.out.println();
InventoryObject[i].PrintInventory();
}
}
public static void PrintMenu()
{
System.out.println();
System.out.println("Display Inventory: <1>");
System.out.println("Display Currency: <2>");
System.out.println("Purchase Item: <3>");
System.out.println("Exit: <4>");
System.out.println();
}
}
class Inventory
{
private int ID;
private String Type;
private String Name;
private String PriceText;
private double Cost;
private String QuantityText;
private int StockAmount;
//Constructor method. values passed to it from the main method.
public Inventory(int ID, String Type, String Name, String PriceText, double Cost, String QuantityText, int StockAmount)
{
this.ID = ID;
this.Type = Type;
this.Name = Name;
this.PriceText = PriceText;
this.Cost = Cost;
this.QuantityText = QuantityText;
this.StockAmount = StockAmount;
}
public void setID(int ID)
{
this.ID = ID;
}
public void setType(String Type)
{
this.Type = Type;
}
public void setName(String Name)
{
this.Name = Name;
}
public void setPriceText(String PriceText)
{
this.PriceText = PriceText;
}
public void setCost(double Cost)
{
this.Cost = Cost;
}
public void setQuantityText(String QuantityText)
{
this.QuantityText = QuantityText;
}
public void setStockAmount(int StockAmount)
{
this.StockAmount = StockAmount;
}
public int getID()
{
return ID;
}
public String getType()
{
return Type;
}
public String getName()
{
return Name;
}
public String getPriceText()
{
return PriceText;
}
public double getCost()
{
return Cost;
}
public String getQuantityText()
{
return QuantityText;
}
public int getStockAmount()
{
return StockAmount;
}
public void PrintInventory()
{
System.out.println(ID + " " + Type + " " + Name + " " + PriceText
+ " " + Cost + " " + QuantityText + " " + StockAmount);
}
}
答案 0 :(得分:0)
你永远不会读一行,你只计算它们:
while (input.hasNextLine())
{
counter = counter + 1;
}
你必须将line = input.readLine();
置于此循环中并相应地更改逻辑,否则,它将始终保持在while循环中。考虑何时需要更新或阅读计数器。
答案 1 :(得分:0)
这是一个无限循环。如果文件中有文本,则输入具有下一行。
由于该行未在循环中读取,因此input.hasNextLine()
始终为真。
while (input.hasNextLine())
{
counter = counter + 1;
}
您应该使用List来阅读对象,这样您最初不需要知道大小。
答案 2 :(得分:0)
您正在阻止来自Inventory.txt
循环中while
文件的输入。您只检查下一行是否存在而没有实际读取下一行。所以你要在这里进行无限循环:
int counter = 0;
while (input.hasNextLine())
{
counter = counter + 1;
}
你需要在whilte循环中的某个时刻调用它:
input.nextLine();
提示:您可以合并在此while
循环中执行的所有操作。你可能不需要那么多循环:
line
;在line
循环之前声明while
。ProductArray
。这将line.split
分配给ProductArray
。ProductArray
变量创建一个。PrintInventory
方法。希望这有帮助!
答案 3 :(得分:0)
理想情况下,这应解决问题。