所以我很长时间以来一直在使用这个程序。该作业要求我使用BufferedReader
,FileReader
和StringTokenizer
读取.txt文件,创建一个数组,然后打印所有信息。我去找我的教授寻求帮助,她为我挖出了一个骨架,但经过一些编辑,这就是我想出来的:
import java.io.*;
import java.util.StringTokenizer;
public class Inventory {
public static void main(String[] args) throws IOException {
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
final int MAX = 500;
InventoryItem[] items = new InventoryItem[MAX];
StringTokenizer tokenizer;
String line, name, file = "inventory.txt";
int price, units, count = 0;
try {
FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);
line = inFile.readLine();
while ((line = inFile.readLine()) != null) {
tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreElements()) {
name = tokenizer.nextToken();
try {
units = Integer.parseInt(tokenizer.nextToken());
price = Integer.parseInt(tokenizer.nextToken());
items[count++] = new InventoryItem(name, units, price);
} catch (NumberFormatException exception) {
System.out.println("Error in input. Line ignored:");
System.out.println(line);
}
line = inFile.readLine();
}
}
inFile.close();
for (int scan = 0; scan < count; scan++)
System.out.println(items[scan]);
} catch (FileNotFoundException exception) {
System.out.println("The file " + file + " was not found.");
} catch (IOException exception) {
System.out.println(exception);
}
stdin.read();
}
}
程序没有问题编译,但是当我运行它时会打印出异常错误行“输入错误。忽略行”,然后打印出.txt文件中的第二行,然后不读取下一行并保持运行。
即使在此之后,作业也要我以某种方式输出数据,如果没有System.out.println
,我不知道怎么办。
这是InventoryItem代码:
import java.util.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class InventoryItem
{
public static String name;
private int units;
private double price;
/**
* Default Constructor
*/
public InventoryItem()
{
this (name, 3, 20.00);
}
/**
* Non-Default Constructor
* Constructs new InventoryItem class with parameters name, units
* and price from main class.
*
* @param name name of item
* @param units number of units
* @param price price of item
*/
public InventoryItem (String name, int units, double price)
{
this.name = name;
this.units = units;
this.price = price;
}
/**
* Returns the name
*
* @return returns name of item
*/
public String getName()
{
return name;
}
/**
* Sets the name
*
* @param name The name of the unit
*/
public void setName(String name)
{
this.name = name;
}
/**
* Returns the number of units
*
* @return returns the number of units
*/
public int getUnits()
{
return units;
}
/**
* Sets the number of units
*
* @param units the number of units
*/
public void setUnits(int units)
{
this.units = units;
}
/**
* Returns the price of each unit
*
* @return returns the price
*/
public double getPrice()
{
return price;
}
/**
* Sets the price
*
* @param price the price of each unit
*/
public void setPrice(double price)
{
this.price = price;
}
/**
* Returns the data in the form of an array
*
* @param name the name of the unit
* @param units the number of units
* @param price the price of each unit
* @return Returns the inputted data in a specified format
*/
public String toString()
{
DecimalFormat fmt = new DecimalFormat("0.00");
return name + ":\t" + units + "\t" + price + "\t" +
fmt.format((units * price));
}
}
答案 0 :(得分:0)
你犯了两个错误。
价格包含小数,因此您不能使用int。浮动是一种可能性(虽然,一般情况下,不推荐):
float price;
//...
price = Float.parseFloat(tokenizer.nextToken());
第二个错误是每行读两次。以下更改避免了这一点:
line = inFile.readLine();
while( line != null ) {
//...
line = inFile.readLine();
}
请注意,您可以简化此操作:
while( (line = inFile.readLine()) != null {
//...
}
要打印项目,请在InventoryItem类中编写合适的方法public String toString()
。
<强>后来强>
必须修复InventoryItem的代码:
public class InventoryItem {
private String name = "";
private int units;
private double price;
public InventoryItem(){
}
public InventoryItem (String name, int units, double price){
this.name = name;
this.units = units;
this.price = price;
}
// ... etc
}
为使用默认构造函数创建的对象提供奇特的值确实没有意义。将空字符串""
分配给name
是合理的,以避免NPE; int和double字段最好留在0。
或者,只需删除默认构造函数。