-1

I am very new to coding, have only completed a few hours of YouTube videos to learn thus far. I am trying to complete a practice code and am facing some trouble.

I have attached a part of the code below. When I am entering value in (10,12,14, and 16) the code is still responding with "Wrong Response". In addition to this the following line is not properly functioning. It is not giving me the option to select a crust type. Please let me know if anyone has any suggestions. Crust problem:

System.out.println("What type of crust would you like? ");
System.out.print("(H)and-tossed, (T)hin-crust, or (D)eep-dish: ");
crust = keyboard.nextLine();

Int Value problem:

 if ( size.equals(" 10 ")) {
  pizzaPrice = SM_Price;
} else if ( size.equals(" 12 ")) {
    pizzaPrice = MED_Price;
} else if ( size.equals(" 14 ")) {
    pizzaPrice = LG_Price;
} else if (size.equals(" 16 ")) {
    pizzaPrice = XL_Price;
}
else { System.out.println("Wrong repsonse. ");

Thank you.

Purdy
  • 15
  • 2
  • Why don't you use `nextInt` and compare integer literals? Otherwise, you are literally comparing space, space, integer, space, space. – Andrew Li Oct 02 '16 at 01:03
  • *Why* do you show us how to input `crust`, but then show code that uses `size`? First part doesn't show us where `size` comes from. Second part doesn't even use `crust`. – Andreas Oct 02 '16 at 01:49

4 Answers4

3

So, keep in mind that the constants 16, "16", " 16 " are all different things and non-equal to each other.

The other thing is that you need to show us what your types are. As Java is statically typed, that type information can actually help determine the behavior that you get.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

if size is an int, then using size against string will not work. i.e size.equals(" 10 ") will not be the same as size.equals(10).

Also will not be the same as size.equals("10") which is different from " 10 "

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

You haven't defined size anywhere, so it's impossible to say.

I suspect your problem however is either that size is a different type that what you're comparing against using equals, or it's because you have spaces before and after your string numbers: " 12 ".

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
0

Please refer to User Input not working with keyboard.nextLine() and String (Java) for your nextLine() problem.

As to your other question one needs to know of what type you declared size to be. If you declared it as String using String size = "10"; for example, then you are quite close:

Just change " 10 " in line 1 to "10" (without whitespace, as "10".equals(" 10 ") == false) and go for the small pizza.

Community
  • 1
  • 1
M.S.
  • 312
  • 1
  • 12