无法使用Scanner输入线路

时间:2015-11-02 13:23:12

标签: java java.util.scanner

我有两个课程:Pizza和PizzaOrder。 PizzaOrder允许用户在他的订单中创建一个包含1-3个比萨饼的新订单。在输入Pizza的数量后,它将继续输入每个披萨的信息。但我有扫描仪的错误,输入比萨的数量后我不能再输入了。

这是我的代码:

类披萨:

public class Pizza {

    public static void main(String[] args){
        Pizza pizza1 = new Pizza("Large", 1, 1, 2);
        Pizza pizza2 = new Pizza("Small", 3, 0, 1);
        Pizza pizza3 = new Pizza("Medium", 1, 1, 3);

        System.out.println(pizza1.getDescription());
        System.out.println(pizza2.getDescription());
        System.out.println(pizza3.getDescription());
    }

    private String size;
    private int numberOfCheese, numberOfPepperoni, numberOfHam;

    public Pizza(String size, int numberOfCheese, int numberOfPepperoni,
            int numberOfHam) {
        super();
        this.size = size;
        this.numberOfCheese = numberOfCheese;
        this.numberOfPepperoni = numberOfPepperoni;
        this.numberOfHam = numberOfHam;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public int getNumberOfCheese() {
        return numberOfCheese;
    }

    public void setNumberOfCheese(int numberOfCheese) {
        this.numberOfCheese = numberOfCheese;
    }

    public int getNumberOfPepperoni() {
        return numberOfPepperoni;
    }

    public void setNumberOfPepperoni(int numberOfPepperoni) {
        this.numberOfPepperoni = numberOfPepperoni;
    }

    public int getNumberOfHam() {
        return numberOfHam;
    }

    public void setNumberOfHam(int numberOfHam) {
        this.numberOfHam = numberOfHam;
    }

    public double calcCost(){
        if(size.equals("Small"))
            return 10 + numberOfCheese*2 + numberOfPepperoni*2 + numberOfHam*2;
        else if(size.equals("Medium"))
            return 12 + numberOfCheese*2 + numberOfPepperoni*2 + numberOfHam*2;
        return 14 + numberOfCheese*2 + numberOfPepperoni*2 + numberOfHam*2;
    }

    public String getDescription(){
        return "Pizza size: "+size + 
                "\nNumber Of Cheese Topping: " + getNumberOfCheese()
                +"\nNumber Of Pepperoni Topping: "+ getNumberOfPepperoni() 
                +"\nNumber Of Ham Topping: "+ getNumberOfHam()
                +"\nPizza cost: " + calcCost()+"$\n"; 
    }
}

类PizzaOrder:

import java.util.Random;
import java.util.Scanner;

public class PizzaOrder {

    public static void main(String[] args){
        PizzaOrder pizzaOrder = new PizzaOrder();
        pizzaOrder.setNumPizzas(pizzaOrder.numPizzas);
    }

    private Pizza pizza1, pizza2, pizza3;
    private int numPizzas;
    private int id;

    public PizzaOrder(){
        System.out.println("Enter the number of Pizza: ");
        Scanner scanner = new Scanner(System.in);
        this.numPizzas = scanner.nextInt();
        scanner.close();
        Random random = new Random();
        int range = 100 - 0 + 1;
        this.id =  random.nextInt(range) + 0;
    }

    public void setNumPizzas(int numPizzas){
        if(numPizzas == 1){
            System.out.println("-----------Enter Pizza1's Information-----------");
            enterInformation(pizza1);
        }
        else if(numPizzas == 2){
            System.out.println("-----------Enter Pizza1's Information-----------");
            enterInformation(pizza1);
            System.out.println("-----------Enter Pizza2's Information-----------");
            enterInformation(pizza2);
        }
        else if(numPizzas == 3){
            System.out.println("-----------Enter Pizza1's Information-----------");
            enterInformation(pizza1);
            System.out.println("-----------Enter Pizza2's Information-----------");
            enterInformation(pizza2);
            System.out.println("-----------Enter Pizza3's Information-----------");
            enterInformation(pizza3);
        }
    }

    public double calcTotal(){
        return pizza1.calcCost() + pizza2.calcCost() + pizza3.calcCost();
    }

    public void enterInformation(Pizza pizza){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the pizza's size: ");
        String size = scanner.nextLine();
        pizza.setSize(size);
        System.out.println("Enter the number of Cheese topping: ");
        int numberOfCheese = scanner.nextInt();
        pizza.setNumberOfCheese(numberOfCheese);
        System.out.println("Enter the number of Pepperoni topping: ");
        int numberOfPepperoni = scanner.nextInt();
        pizza.setNumberOfPepperoni(numberOfPepperoni);
        System.out.println("Enter the number of Ham topping: ");
        int numberOfHam = scanner.nextInt();
        pizza.setNumberOfHam(numberOfHam);
        scanner.close();
        System.out.println("This pizza cost: " + pizza.calcCost() +"$");
    }
}

当我在PizzaOrder类中输入Pizza == 1的数量时,它显示错误:

Enter the number of Pizza: 
1
-----------Enter Pizza1's Information-----------
Exception in thread "main" java.lang.NullPointerException
    at bai2.PizzaOrder.enterInformation(PizzaOrder.java:69)
    at bai2.PizzaOrder.setNumPizzas(PizzaOrder.java:30)
    at bai2.PizzaOrder.main(PizzaOrder.java:10)

如何解决这个问题?

4 个答案:

答案 0 :(得分:1)

您应首先创建Pizza个实例,然后在pizza.setSize

中访问其成员函数,例如enterInformation

在每个if语句中你都可以;

pizza1 = new Pizza()
System.out.println("-----------Enter Pizza1's Information-----------");
enterInformation(pizza1);

此外,您pizzaOrder.numPizzas已经setNumPizzas,因此再次传递它是多余的;

pizzaOrder.setNumPizzas(pizzaOrder.numPizzas);

我建议你为多个Pizza实例使用数组。

答案 1 :(得分:0)

不应该使用多个Scanner实例包装输入流 System.in ,因为一旦你关闭它,它就不应该再次使用

而是使用System.in声明一个Scanner实例。您可以声明一个包含扫描程序全局实例的Utils类。或者只是将一个公共静态的Scanner实例放在Pizza或PizzaOrder中。 - 只要它可以在全球范围内访问,无论哪种方式都有效。

public InputUtil {
    public static Scanner scanner = new Scanner(System.in);
}

用作

InputUtil.scanner.nextInt();

答案 2 :(得分:0)

除了没有初始化pizza1,2,3的实例之外,问题在于你正在关闭流

下面

public static void main(String[] args){
    PizzaOrder pizzaOrder = new PizzaOrder();
    pizzaOrder.setNumPizzas(pizzaOrder.numPizzas);
}

public PizzaOrder(){
    ...
    scanner.close();
    ...
}

然后从setNumPizza拨打enterInformation

public void enterInformation(Pizza pizza){
    Scanner scanner = new Scanner(System.in);
    ...
}

调用scanner.close后,它将关闭基础流。创建单个实例

答案 3 :(得分:-1)

我通过scanner.close()

替换scanner.nextLine()来修复它

以下是我在PizzaOrder课程中的代码:

import java.util.Random;
import java.util.Scanner;

public class PizzaOrder {

    public static void main(String[] args){
        PizzaOrder pizzaOrder = new PizzaOrder();
        pizzaOrder.setNumPizzas(pizzaOrder.numPizzas);
        pizzaOrder.getPizzaOrderDescription();
    }

    private Pizza pizza1, pizza2, pizza3;
    private int numPizzas;
    private int id;

    public PizzaOrder(){
        System.out.println("Enter the number of Pizza: ");
        Scanner scanner = new Scanner(System.in);
        this.numPizzas = scanner.nextInt();
        scanner.nextLine();
        Random random = new Random();
        int range = 100 - 0 + 1;
        this.id =  random.nextInt(range) + 0;
    }

    public void setNumPizzas(int numPizzas){
        if(numPizzas == 1){
            pizza1 = new Pizza("", 1, 1, 1);
            System.out.println("-----------Enter Pizza1's Information-----------");
            enterInformation(pizza1);
        }
        else if(numPizzas == 2){
            pizza1 = new Pizza("", 1, 1, 1);
            System.out.println("-----------Enter Pizza1's Information-----------");
            enterInformation(pizza1);
            pizza2 = new Pizza("", 1, 1, 1);
            System.out.println("-----------Enter Pizza2's Information-----------");
            enterInformation(pizza2);
        }
        else if(numPizzas == 3){
            pizza1 = new Pizza("", 1, 1, 1);
            System.out.println("-----------Enter Pizza1's Information-----------");
            enterInformation(pizza1);
            pizza2 = new Pizza("", 1, 1, 1);
            System.out.println("-----------Enter Pizza2's Information-----------");
            enterInformation(pizza2);
            pizza3 = new Pizza("", 1, 1, 1);
            System.out.println("-----------Enter Pizza3's Information-----------");
            enterInformation(pizza3);
        }
    }

    public double calcTotal(){
       if(numPizzas == 1)
          return pizza1.calcCost();
       else if(numPizzas == 2)
          return pizza1.calcCost() + pizza2.calcCost();
       return pizza1.calcCost() + pizza2.calcCost() + pizza3.calcCost();
    }

    public void enterInformation(Pizza pizza){
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the pizza's size: ");
        String size = scanner.nextLine();
        pizza.setSize(size);
        System.out.println("Enter the number of Cheese topping: ");
        int numberOfCheese = scanner.nextInt();
        pizza.setNumberOfCheese(numberOfCheese);
        System.out.println("Enter the number of Pepperoni topping: ");
        int numberOfPepperoni = scanner.nextInt();
        pizza.setNumberOfPepperoni(numberOfPepperoni);
        System.out.println("Enter the number of Ham topping: ");
        int numberOfHam = scanner.nextInt();
        pizza.setNumberOfHam(numberOfHam);
        scanner.nextLine();
        System.out.println("This pizza cost: " + pizza.calcCost() +"$");
    }

    public void getPizzaOrderDescription(){
        System.out.println("Your total cost is: " + calcTotal() +"$");
    }
}

输出:

Enter the number of Pizza: 
3
-----------Enter Pizza1's Information-----------
Enter the pizza's size: 
Large
Enter the number of Cheese topping: 
1
Enter the number of Pepperoni topping: 
1
Enter the number of Ham topping: 
1
This pizza cost: 20.0$
-----------Enter Pizza2's Information-----------
Enter the pizza's size: 
Large
Enter the number of Cheese topping: 
1
Enter the number of Pepperoni topping: 
1
Enter the number of Ham topping: 
1
This pizza cost: 20.0$
-----------Enter Pizza3's Information-----------
Enter the pizza's size: 
Small
Enter the number of Cheese topping: 
2
Enter the number of Pepperoni topping: 
2
Enter the number of Ham topping: 
2
This pizza cost: 22.0$
Your total cost is: 62.0$