PDA

Click to See Complete Forum and Search --> : [RESOLVED] Simple Java help


stettybet0
Apr 16th, 2007, 07:48 PM
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!


import java.io.*;

public class pizza {

public static void main (String[] args) {

System.out.print("Pizza Size (S, M, or L): ");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

// Ask for user input... in this case pizza size

String pizzaSize = null;
String pizzaCost = null;
String pizzaType = null;
Boolean noPizza = null;

try {
pizzaSize = br.readLine();
} catch (IOException ioe) {
System.out.println("Error!");
System.exit(1);
}

// Take user input, and store it in a string. You have to handle the exception!

if (pizzaSize == "S" || pizzaSize == "s") {
pizzaType = "Small";
pizzaCost = "6.99";
noPizza = false;
} else if (pizzaSize == "M" || pizzaSize == "m") {
pizzaType = "Medium";
pizzaCost = "8.99";
noPizza = false;
} else if (pizzaSize == "L" || pizzaSize == "l") {
pizzaType = "Large";
pizzaCost = "10.99";
noPizza = false;
} else {
noPizza = true;
}

// Use an If-Then-Else statement to determine what action to take based on the user input.
// Remember the user input is stored in string "pizzaSize".

if (noPizza == false) {
System.out.println("A " + pizzaType + " pizza will cost $" + pizzaCost + ".");
} else if (noPizza == true) {
System.out.println("Sorry, we don't have that pizza size.");
}

// Output the cost of pizza based on the type of pizza inputed.
// If they input anything other than S, M, or L, it will tell them that you don't have that pizza size.

}
}

lunchboxtheman
Apr 16th, 2007, 10:13 PM
When comparing Strings you need to use the .equals() method, not ==.

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

stettybet0
Apr 17th, 2007, 04:24 PM
thanks!