0

So my problem is that I want to ask for input for using Scanner, but it just doesn't print out at all. And when it displays the value for the skipped Scanner, Scanner cheeseType = new Scanner(System.in);, I get null.

package classesProject;

import java.util.Scanner;

class Pizza {

    String size;
    String cheese;
    int numToppings;
    double price = 0;

}
public class pizzaTime {

    public static void main(String[] args) {

        Pizza order1 = new Pizza();

        double priceOfSize = 0;
        double priceOfCheese = 0;
        double priceOfToppings = 0;

        System.out.println("Pizza size small, medium or large: ");
        Scanner sizeAsker = new Scanner(System.in);
        order1.size = sizeAsker.nextLine();

        if(order1.size == "small") {
            priceOfSize = 3.0;

        } else if(order1.size == "medium") {
            priceOfSize = 5.0;

        } else if(order1.size == "large") {
            priceOfSize = 7.0;

        System.out.println("Cheese type: normal, xtra, or thic: ");
        Scanner cheeseType = new Scanner(System.in);
        order1.cheese = cheeseType.nextLine();

        }if(order1.cheese == "normal") {
            priceOfCheese = 0.0;

        } else if(order1.cheese == "xtra") {
            priceOfCheese = 0.5;

        } else if(order1.cheese == "thic") {
            priceOfCheese = 1.0;

        }

        System.out.println("Number of toppings: ");
        Scanner toppingAsker = new Scanner(System.in);
        order1.numToppings = toppingAsker.nextInt();

        priceOfToppings = order1.numToppings * 0.25;

        double orderTotalPrice = priceOfSize + priceOfCheese;

        System.out.println("Pizza size: " + order1.size + "\n"
                + "Cheese type: " + order1.cheese + "\n"
                + "Number of toppings: " + order1.numToppings + "\n"
                + "Order total: " + orderTotalPrice
                );

    }

}

The one that gets skipped is:

System.out.println("Cheese type: normal, xtra, or thic: ");
Scanner cheeseType = new Scanner(System.in);
order1.cheese = cheeseType.nextLine();

Upon running, the console displays:

Pizza size small, medium or large: 
small
Number of toppings: 
2
Pizza size: small
Cheese type: null
Number of toppings: 2
Order total: 0.0

So as you can see, it jumps straight from the pizza size Scanner to the number of toppings Scanner, rather than going in the sequential order. I don't have any idea why, or what I should do to fix this.

  • not entirely sure, but there are very basic errors in your code, that might have a bigger impact than you think: (order1.cheese == "normal") this is not the right way to compare Strings, or any Object for that matter. use the equals method – Stultuske Jan 23 '19 at 08:09
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Stultuske Jan 23 '19 at 08:10
  • Consider using the same instance of `Scanner` everywhere, don't create new ones each time. – Arnaud Jan 23 '19 at 08:14

2 Answers2

1

Since you are closing the bracket after the next question, this one is only asked with large pizza's

else if(order1.size == "large") {
        priceOfSize = 7.0;

    System.out.println("Cheese type: normal, xtra, or thic: ");
    Scanner cheeseType = new Scanner(System.in);
    order1.cheese = cheeseType.nextLine();

    }

If you would put a closing bracket after priceOfSize = 7, you can continue. You will still have to fix the missing bracket elsewhere though.

Stefan
  • 2,098
  • 2
  • 18
  • 29
  • that's not the main problem there. the code compiles, so it does run. sure the bracket might be misplaced, but for some values they should get the right result. the main problem is the referential comparison of Objects – Stultuske Jan 23 '19 at 08:17
  • @Stultuske although your statement is correct, it doesn't address the problem the author was facing. His statement was not reached because of this bracket issue. Maybe I should have added extensive comments about the other coding problems his code has. I will consider that in the future. – Stefan Jan 23 '19 at 11:45
0

It's not a good idea to use multiple scanners on the same input stream (System.in in your case). Make only one scanner in the beginning of the method:

Scanner scanner = new Scanner(System.in);
//read input with this here

scanner.close(); //also close it

Edit: Stefan's answer is the correct one, but this is still a good idea.

Gtomika
  • 845
  • 7
  • 24