Results 1 to 4 of 4
  1. #1
    Denis2k11 is offline Member
    Join Date
    Oct 2012
    Posts
    35
    Rep Power
    0

    Default Can't get the hang of methods!! What's wrong with my code??

    I'm trying to have a method that will ask in a menu option 1 or 2. Option 2 should make the scanner print out ''this is the load method'' but otpion 1 should ask for choose numbers1-3. However, everytime I execute it, the ''this is the laod method'' shows up if I press option 1 and choose any number. Why is this!? :(

    Java Code:
    import javax.swing.JOptionPane;
    public class Menutesting
    {
    		static Object[] menu = {"Option 1.", "Option 2.", "Exit"};
    		static Object[] menuSecondLevel = {"Choose number 1","Choose number 2","Choose number 3", "Exit"};
    	public static void main(String[] args)
    	{
    		{
         		Object options = JOptionPane.showInputDialog(null,"Menu testing:", "Menu", 3, null, menu, menu[0]);
         		if (options == null || options.equals("Exit"))
    						System.out.print("The program is terminated");
          		else if(options.equals("Option 1."))
          		{
    	      		MethodCreate();
          		}
          		else if(choice.equals("Option 2."));
          		{
    	      		MethodLoad();
          		}
      		}
    	}
    	public static void MethodCreate()
    	{
    		Object chooseOption = JOptionPane.showInputDialog(null,"Specify option:", "Menu", 3, null, menuSecondLevel, menuSecondLeve;[0]);
    		System.out.print(chooseOption);
    	}
    	public static void MethodLoad()
    	{
    		System.out.print("This is the load method");
    	}
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Can't get the hang of methods!! What's wrong with my code??

    Java Code:
    else if(choice.equals("Option 2.")); <------
    You've got a rogue ';' there which is short-circuiting your if statement.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Can't get the hang of methods!! What's wrong with my code??

    I am not even certain how this compiles. The object "choice" does not seem to be declared anywhere. In MethodCreate() you have a typo in the spelling of menuSecondLeve;[0].

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Default Re: Can't get the hang of methods!! What's wrong with my code??

    Enjoy fully fixed and functional
    Java Code:
    import javax.swing.JOptionPane;
    public class Menutesting
    {
        static Object[] menu = {"Option 1.", "Option 2.", "Exit"};
        static Object[] menuSecondLevel = {"Choose number 1","Choose number 2","Choose number 3", "Exit"};
        public static void main(String[] args)
        {
            {
                Object options = JOptionPane.showInputDialog(null,"Menu testing:", "Menu", 3, null, menu, menu[0]);
                if (options == null || options.equals("Exit"))
                            System.out.print("The program is terminated");
                else if(options.equals("Option 1."))
                {
                    MethodCreate();
                }
                else if(options.equals("Option 2."))
                {
                    MethodLoad();
                }
            }
        }
        public static void MethodCreate()
        {
            Object chooseOption = JOptionPane.showInputDialog(null,"Specify option:", "Menu", 3, null, menuSecondLevel, menuSecondLevel);
            System.out.print(chooseOption);
        }
        public static void MethodLoad()
        {
            System.out.print("This is the load method");
        }
    }

Similar Threads

  1. Replies: 5
    Last Post: 04-14-2012, 12:18 PM
  2. code gets hang
    By rungtashubham in forum AWT / Swing
    Replies: 4
    Last Post: 03-22-2012, 07:40 PM
  3. What went wrong. Methods problem.
    By ocean in forum New To Java
    Replies: 5
    Last Post: 11-02-2009, 06:28 PM
  4. what's wrong with my code? please help me...
    By lovely23 in forum Java Applets
    Replies: 2
    Last Post: 03-01-2009, 01:33 PM
  5. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 08:27 AM

Posting Permissions

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