Java 读取要打印到屏幕的文件
我有一个文件Inventory.txt,我正试图打印到屏幕上。文件中的每一行都进入一个对象数组。我的代码编译时没有错误,但是当我运行它时,没有任何东西会在屏幕上打印任何东西。Im使用Mac和文本编辑/终端Java 读取要打印到屏幕的文件,java,readfile,Java,Readfile,我有一个文件Inventory.txt,我正试图打印到屏幕上。文件中的每一行都进入一个对象数组。我的代码编译时没有错误,但是当我运行它时,没有任何东西会在屏幕上打印任何东西。Im使用Mac和文本编辑/终端 import java.util.Scanner; import java.io.*; public class VendingMachineSimulator { to be imported public static void main(String[] args)
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);
}
}
import java.util.Scanner;
导入java.io.*;
公共类自动售货机模拟器
{
进口
公共静态void main(字符串[]args)引发FileNotFoundException
{
文件InventoryFile=新文件(“Inventory.txt”);
扫描仪输入=新扫描仪(目录文件);
//此代码块将统计文本文件中的行数(产品)
int计数器=0;
while(input.hasNextLine())
{
计数器=计数器+1;
}
存货[]存货对象=新存货[计数器];
字符串行=”;
对于(int i=0;i
你从来没有读过一行,你只会数一数:
while (input.hasNextLine())
{
counter = counter + 1;
}
您必须放置
line=input.readLine()
在这个循环中的某个地方,并相应地更改您的逻辑,否则,它将始终停留在while循环中。想想什么时候需要更新或读取计数器 你从来没有读过一行,你只会数一数:
while (input.hasNextLine())
{
counter = counter + 1;
}
您必须放置
line=input.readLine()
在这个循环中的某个地方,并相应地更改您的逻辑,否则,它将始终停留在while循环中。想想什么时候需要更新或读取计数器 这是一个无止境的循环。如果文件中有文本,则输入有下一行。
由于循环中不读取该行,input.hasNextLine()
始终为真
while (input.hasNextLine())
{
counter = counter + 1;
}
您应该使用列表来读取对象,这样您就不需要一开始就知道对象的大小。这是一个无休止的循环。如果文件中有文本,则输入有下一行。 由于循环中不读取该行,
input.hasNextLine()
始终为真
while (input.hasNextLine())
{
counter = counter + 1;
}
您应该使用列表来读取对象,这样您就不需要一开始就知道对象的大小。您在
循环时阻塞了Inventory.txt
文件中的输入。只检查下一行是否存在,而不实际读取下一行。所以你将在这里进入一个无限循环:
int counter = 0;
while (input.hasNextLine())
{
counter = counter + 1;
}
您需要在whilte循环中的某个点调用此函数:
input.nextLine();
提示:您可以在while
循环中合并您正在执行的所有操作。您可能不需要那么多的循环:
分配到行
;在while
循环之前声明行
将行拆分为ProductArray
。这将是line.split
分配给ProductArray
创建新的库存对象。您可能不需要库存对象数组。只需使用ProductArray
变量创建一个
打印库存。只需在步骤3中创建的对象上调用PrintInventory
方法
希望这有帮助 您正在阻止while
循环中Inventory.txt
文件的输入。只检查下一行是否存在,而不实际读取下一行。所以你要去参加一个