Results 1 to 4 of 4
- 02-27-2013, 04:47 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 24
- Rep Power
- 0
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"); } }
- 02-27-2013, 05:11 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Can't get the hang of methods!! What's wrong with my code??
You've got a rogue ';' there which is short-circuiting your if statement.Java Code:else if(choice.equals("Option 2.")); <------Please do not ask for code as refusal often offends.
- 02-27-2013, 05:14 PM #3
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 690
- Rep Power
- 1
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].
JimThe Java™ Tutorial
YAT -- Yet Another Typo
- 02-28-2013, 01:42 AM #4
Member
- Join Date
- Jan 2013
- Location
- San Antontio, Texas, US
- Posts
- 11
- Rep Power
- 0
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
-
Breaking up code into methods. Can't figure out how to code
By Log1c in forum New To JavaReplies: 5Last Post: 04-14-2012, 12:18 PM -
code gets hang
By rungtashubham in forum AWT / SwingReplies: 4Last Post: 03-22-2012, 07:40 PM -
What went wrong. Methods problem.
By ocean in forum New To JavaReplies: 5Last Post: 11-02-2009, 06:28 PM -
what's wrong with my code? please help me...
By lovely23 in forum Java AppletsReplies: 2Last Post: 03-01-2009, 01:33 PM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks