CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2009
    Posts
    105

    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.

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: What am i doing wrong in this code?

    Do not use == to compare Strings, use the equals or equalsIgnoreCase methods instead.

  3. #3
    Join Date
    Jul 2009
    Posts
    105

    Re: What am i doing wrong in this code?

    Thank you so much is there a thank button or anything? xD

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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 code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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

Featured