Java 库存项目。数组

标签 java arrays object storage tostring

所以我试图弄清楚如何让我的代码工作,但我不断收到 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());
     }
  }

当然,这是驱动程序类,这是我的对象:

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

最佳答案

尝试这个更新的代码:

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

关于Java 库存项目。数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40056111/

相关文章:

java - 即使我设置为 true,ZipFile.isSplitArchive 仍返回 false

c - 如何合并排序字符?

javascript - JavaScript 的 `Object.length` 构造函数上的 `Object` 属性有什么意义?

java - 创建一个与另一个与原始对象有关系的对象有什么含义?

java - 在数组中查找单个整数

java - 具有复杂对象的@Repeat Form Helper - Play Framework

java - 在文件中保存对象产生错误 : "FileOutputStream cannot be resolved to a type"

arrays - 在类中实例化对象数组

c++ - 如何制作一个数组,其中的每个数字都存储一个字符串,就像 const char * argv[ ] 那样?

Java:对象分配