So I have this program that should read a txt file about an item inventory, problem is when i run it i get this "The file inventory.txt was not found.", i have it all in the same folder, the java file, class file, and txt file, but nothing happens still.
(所以我有一个程序,该程序应该读取有关物品清单的txt文件,问题是当我运行它时,我得到的是“找不到文件清单.txt。”,我全部都放在同一个文件夹中,即java文件,类文件和txt文件,但仍然没有任何反应。)
Here's the code: (这是代码:)
import java.util.StringTokenizer;
import java.io.*;
import java.text.DecimalFormat;
class InventoryItem {
private String name;
private int units; // number of available units of this item
private float price; // price per unit of this item
private DecimalFormat fmt;
public InventoryItem (String itemName, int numUnits, float cost) {
name = itemName;
units = numUnits;
price = cost;
fmt = new DecimalFormat ("0.##");
}
public String toString() {
return name + ":" + units + " at " + price + " = " +
fmt.format ((units * price));
}
}
public class Inventory{
// Reads data about a store inventory from an input file,
// creating an array of InventoryItem objects, then prints them.
public static void main (String[] args) {
final int MAX = 100;
InventoryItem[] items = new InventoryItem[MAX];
StringTokenizer tokenizer;
String line, name, file="inventory.txt";
int units, count = 0;
float price;
try{
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader (fr);
line = inFile.readLine();
while (line != null) {
tokenizer = new StringTokenizer (line);
name = tokenizer.nextToken();
try {
units = Integer.parseInt (tokenizer.nextToken());
price = Float.parseFloat (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);
}
}
}
and now here is the txt file i want it to read:
(现在这是我要读取的txt文件:)
Widget 14 3.35 Spoke 132 0.32 Wrap 58 1.92 Thing 28 4.17 Brace 25 1.75 Clip 409 0.12 Cog 142 2.08
ask by SpaceSpaghetti translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…