盘点程序只读一行?

Inventory program only reads one line?

所以我在这个程序上遇到问题已经有很长一段时间了。作业要求我使用 BufferedReaderFileReaderStringTokenizer 读取一个 .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();
    }
}

程序编译没有问题,但是当我运行它打印出异常错误行"Error in input. Line ignored",然后打印出第二行在我的 .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));
   }

}

你犯了两个错误。

价格包含小数点,所以不能使用整数。 float 是一种可能性(尽管通常不推荐):

float price;
//...
      price = Float.parseFloat(tokenizer.nextToken());

第二个错误是每行读取两次。以下更改避免了这种情况:

   line = inFile.readLine();     
   while( line != null ) {
       //...
       line = inFile.readLine();
   }

请注意,您可以对此进行简化:

   while( (line = inFile.readLine()) != null {
       //...
   }

要打印您的物品,请在您的 InventoryItem class 中写一个合适的方法 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。

或者,只需删除默认构造函数。