|
-
January 2nd, 2011, 04:55 PM
#1
What am i doing wrong in this code?
I am having problems with a very basic menu program and can't seem to figure out why i not getting the proper return:
Code:
import java.util.Scanner;
public class WellnessMenu
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String choice;
System.out.println("Please select one of the following options from the menu for more information.");
System.out.println("\nSelect a letter corresponding to a menu option.");
System.out.println("[A] BMI");
// provide menu item for BMR
System.out.println("[B] BMR");
// provide menu item for Healthy Eating
System.out.println("[C] Healthy Eating");
// provide menu item for Food Pyramid
System.out.println("[D] Food Pyramid");
// provide menu item for Fitness Training
System.out.println("[E] Fitness Training");
// prompt user to enter A, B, C, D, or E: ");
choice = in.nextLine();
// accept user choice with a Scanner class method
if(choice == "A" || choice == "a") //condition for choice A goes in the parentheses
{
System.out.println("Testing: You chose A for BMI");
}
else if(choice == "B" || choice == "b") //condition for choice B goes in the parentheses
{
// provide print statement to indicate menu item B was chosen
System.out.println("Testing: You chose B for BMR");
}
else if (choice == "C" || choice == "c") //condition for choice C goes in the parentheses
{
// provide print statement to indicate menu item C was chosen
System.out.println("Testing: You chose C for Healthy Eating");
}
else if (choice == "D" || choice == "d") //condition for choice D goes in the parentheses
{
// provide print statement to indicate menu item D was chosen
System.out.println("Testing: You chose D for Food Pyramid");
}
else if (choice == "E" || choice == "e") //condition for choice E goes in the parentheses
{
// provide print statement to indicate menu item E was chosen
System.out.println("Testing: You chose E for Fitness Training");
}
else //default choice for an invalid entry
{
// provide print statement to indicate invalid entry
System.out.println("Testing: You chose an invalid option.");
}
}
}
If i type in "a" or "A" it doesn't work. Same with all the other possible letters
P.S. simple java not advanced.
-
January 2nd, 2011, 07:56 PM
#2
Re: What am i doing wrong in this code?
Do not use == to compare Strings, use the equals or equalsIgnoreCase methods instead.
-
January 2nd, 2011, 08:48 PM
#3
Re: What am i doing wrong in this code?
Thank you so much is there a thank button or anything? xD
-
January 3rd, 2011, 05:07 AM
#4
Re: What am i doing wrong in this code?
In the top right hand corner of each post there is a "Rate this Post" link which you can use to add to jcaccia reputation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|