Results 1 to 3 of 3

Thread: [RESOLVED] Simple Java help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Resolved [RESOLVED] Simple Java help

    Not sure what is wrong with my simple script, but no matter what I input, the output is always "Sorry, we don't have that pizza size.".

    Help please. This is to teach a friend, and it's gonna be embarrassing if I teach him wrong!

    java Code:
    1. import java.io.*;
    2.  
    3. public class pizza {
    4.  
    5.    public static void main (String[] args) {
    6.  
    7.       System.out.print("Pizza Size (S, M, or L): ");
    8.  
    9.       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    10.  
    11.       // Ask for user input... in this case pizza size
    12.  
    13.       String pizzaSize = null;
    14.       String pizzaCost = null;
    15.       String pizzaType = null;
    16.       Boolean noPizza = null;
    17.  
    18.       try {
    19.          pizzaSize = br.readLine();
    20.       } catch (IOException ioe) {
    21.          System.out.println("Error!");
    22.          System.exit(1);
    23.       }
    24.  
    25.       // Take user input, and store it in a string. You have to handle the exception!
    26.  
    27.       if (pizzaSize == "S" || pizzaSize == "s") {
    28.          pizzaType = "Small";
    29.          pizzaCost = "6.99";
    30.          noPizza = false;
    31.       } else if (pizzaSize == "M" || pizzaSize == "m") {
    32.          pizzaType = "Medium";
    33.          pizzaCost = "8.99";
    34.          noPizza = false;
    35.       } else if (pizzaSize == "L" || pizzaSize == "l") {
    36.          pizzaType = "Large";
    37.          pizzaCost = "10.99";
    38.          noPizza = false;
    39.       } else {
    40.          noPizza = true;
    41.       }
    42.  
    43.       // Use an If-Then-Else statement to determine what action to take based on the user input.
    44.       // Remember the user input is stored in string "pizzaSize".
    45.  
    46.       if (noPizza == false) {
    47.          System.out.println("A " + pizzaType + " pizza will cost $" + pizzaCost + ".");
    48.       } else if (noPizza == true) {
    49.          System.out.println("Sorry, we don't have that pizza size.");
    50.       }
    51.  
    52.       // Output the cost of pizza based on the type of pizza inputed.
    53.       // If they input anything other than S, M, or L, it will tell them that you don't have that pizza size.
    54.  
    55.    }
    56. }
    Last edited by stettybet0; Apr 16th, 2007 at 07:54 PM.

  2. #2
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Simple Java help

    When comparing Strings you need to use the .equals() method, not ==.

    Code:
    if (pizzaSize.equals("S") || pizzaSize.equals("s"))

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    122

    Re: Simple Java help

    thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width