gpt4 book ai didi

java - 读取文件以打印到屏幕

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:24 24 4
gpt4 key购买 nike

我有一个文件 Inventory.txt,我正在尝试将其打印到屏幕上。文件中的每一行都进入一个对象数组。我的代码编译时没有错误,但是当我运行它时,屏幕上什么也没有打印。我使用 Mac 和 TextEdit/Terminal。

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);
}




}

最佳答案

你从来没有读过一行,你只是计算它们:

while (input.hasNextLine())
{
counter = counter + 1;
}

您必须将 line = input.readLine(); 放置在该循环内的某个位置并相应地更改您的逻辑,否则,它将始终停留在 while 循环中。考虑一下何时需要更新或读取计数器。

关于java - 读取文件以打印到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43134464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com