Java 库存项目。数组

Java Inventory Project. Arrays

所以我想弄清楚如何让我的代码工作,但我一直收到 nullError。我没有正确存储我的数据吗?这是我的代码:

public class ProductTesterPart1{
public static void main(String[] args) {
     Scanner userInput = new Scanner(System.in);
     System.out.println("Please enter the product number, name, stock, and price in that order.");

     List<ProductPart1> products = new ArrayList<ProductPart1>();
     String line = userInput.nextLine();
     while (!line.equals("quit")) {
        if (line == null || line.trim().isEmpty()) {
           products.add(new ProductPart1());
        }
        else {
           try {
              Scanner s = new Scanner(line);
              int number = s.nextInt();
              String name = s.next();
              int stock = s.nextInt();
              double price = s.nextDouble();

              products.add(new ProductPart1(number, name, stock, price));
           }
           catch (NoSuchElementException e) {
              System.out.print("Error: " + e.getMessage());
           }
        }
     }

     for (ProductPart1 p : products) {
          System.out.println(p.toString());
     }
  }

当然,那是 driver class,这是我的 object:

public class ProductPart1 {

//Declares variables
private int productNumber;
private String productName;
private int productStock;
private double productPrice;

//Constructor
public ProductPart1(){
    setNumber(0);
    productName = "Null";
    productStock = 0;
    productPrice = 0.0;
}

//Overload constructor
public ProductPart1(int number, String name, int stock, double price){
    productNumber = number;
    productName = name;
    productStock = stock;
    productPrice = price;
}

//set the number of the object
public void setNumber(int newNumber){
    productNumber = newNumber;
}

//set the name of the object
public void setName(String newName){
    productName = newName;
}

//set the stock of an object
public void setStock(int newStock){
    productStock = newStock;
}

//set the price of an object
public void setPrice(double newPrice){
    productPrice = newPrice;
}

//get the number of an object
public int getNumber(){
    return getNumber();
}

//get the name of an object
public String getName(){
    return productName;
}

//get the stock of an object
public int getStock(){
    return productStock;
}

//get the price of an object
public double getPrice(){
    return productPrice;
}

//toString to bring the object together.
public String toString(){
    return new String("Number: " + getNumber() + " Name: " + productName + " Price: " + productPrice + " Stock: " + productStock);
}

您的代码创建了一个无限循环。首先,您阅读产品编号、名称、库存和价格行。然后你进入一个 while 循环,在那里你读出这一行,但再也不会改变行变量,所以它会被无限地一次又一次地读取。

试试这个更新后的代码:

public class ProductTesterPart1{
public static void main(String[] args) {
     Scanner userInput = new Scanner(System.in);
     System.out.println("Please enter the product number, name, stock, and price in that order.");

     List<ProductPart1> products = new ArrayList<ProductPart1>();
     String line = userInput.nextLine();
     while (!line.equals("quit")) {
         if (line == null || line.trim().isEmpty()) {
             products.add(new ProductPart1());
         }
         else {
             try {
                 Scanner s = new Scanner(line);
                 int number = s.nextInt();
                 String name = s.next();
                 int stock = s.nextInt();
                 double price = s.nextDouble();
                 products.add(new ProductPart1(number, name, stock, price));
             } catch (NoSuchElementException e) {
                 System.out.print("Error: " + e.getMessage());
             }
        }
        if(userInput.hasNext()){
            line = userInput.nextLine();
        } else {
            break;
        }
     }

     for (ProductPart1 p : products) {
          System.out.println(p.toString());
     }
  }
}