Results 1 to 13 of 13
Thread: Switch JOptionPane
- 10-24-2010, 01:37 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
Switch JOptionPane
I need to write a switch statement to let user select one operation out of three methods using JOptionPane. The methods are: second, last, first. I think it would be something like this:
public static void main(String[] args)
{
SLF3 Benjamin = new SLF3();
int userSelection;
userSelection = JOptionPane.showInputDialog(" ");
switch (userSelection)
{
case 1: Benjamin.second();
break;
case 2: Benjamin.last();
break;
case 3: Benjamin.first();
break;
}
}
What is the simplest way to use JOptionPane.showInputDialog() to select one of three options? How do you use case statements to call methods from your program? Would I need break statements in this instance?
-
Have you tried the code? Does it work? Have you tried it with and without break statements? Experimenting is often the easiest way of answering such questions and often teaches you more than your teachers or we can.
Luck!
- 10-24-2010, 02:53 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
I have tried the code as is. It did not compile, error was incompatible types: found string, required int. I understand that type string and int are not the same. In order to apply arithmeticExpression doesn't the type need to be numeric? How would you get a numeric input from JOptionPane? How do you format JOptionPane to give user selection of three options?
- 10-24-2010, 03:16 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
The JOptionPane will return a String. You might convert it to an int with the Integer method parseInt().
- 10-24-2010, 03:21 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
I am reading in textbook about JOptionPane Option Dialog. I will look into parseInt as well.
- 10-24-2010, 03:25 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
showOptionDialog() is probably the neatest way of coding it - but it's also more complex than a simple input dialog.
- 10-24-2010, 12:17 PM #7
Other than the parsing error, the code looks fine to me. So it should work after you convert the String from the JOption into an integer.
Sincerely, Joshua Green
Please REP if I help :)
- 10-24-2010, 04:40 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
Ok I have this now:
public static void main(String[] args)
{
SLF3 Benjamin = new SLF3();
int userSelection = JOptionPane.showOptionDialog(null, "Select an operation", "Option Dialog", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, new Object[]{"Second Char", "Last Char", "First 3 Char"}, " ");
switch (userSelection)
{
case 1: Benjamin.last();
break;
case 2: Benjamin.last();
break;
case 3: Benjamin.first();
break;
}
}
This compiles with no errors but the button "Last Char" is the only one that works properly. I don't understand how to connect the option buttons to the individual case statements or how to use the case statements to call the method. The textbook does not explain this.
- 10-24-2010, 04:57 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
The option index being returned is zero based so your option pane returns 0, 1 or 2; your switch case numbers are incorrect (and so are the statements in your case clauses as well as the option texts, in short: your handling of the different options is a mess).
kind regards,
Jos
- 10-24-2010, 05:08 PM #10
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Java Code:public static void main(String[] args) { // public static String showInputDialog(Object message) // As you can see the showInputDialog() method returns a String // So assign a String type to its input value String userSelection = JOptionPane.showInputDialog(" "); // parseInt() Parses the string argument as a signed decimal integer. int selected=Integer.parseInt(userSelection); System.out.println(selected); // now you can use your switch(selected) }
- 10-24-2010, 05:18 PM #11
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
Which part of the option dialog is the option index? My book is not very helpful it only shows the code and doesn't explain the details. How do you use the case clause statement to call a method? Can you give an example? What is wrong with the option text?
- 10-24-2010, 05:29 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
I would've expected something like this in your switch:
Pay close attention to the case numbers and the methods being called.Java Code:switch (userSelection) { case 0: Benjamin.last(); break; case 1: Benjamin.middle(); break; case 2: Benjamin.first(); break; }
kind regards,
Jos
- 10-24-2010, 05:34 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
if else changes to switch-case?
By noobinoo in forum New To JavaReplies: 1Last Post: 04-23-2010, 05:56 PM -
switch on a string
By dinosoep in forum New To JavaReplies: 14Last Post: 02-16-2010, 10:31 AM -
switch
By dj kourampies in forum New To JavaReplies: 17Last Post: 01-30-2009, 05:32 PM -
switch
By dj kourampies in forum New To JavaReplies: 2Last Post: 01-30-2009, 08:46 AM -
Switch help please!!!!
By soc86 in forum New To JavaReplies: 6Last Post: 11-23-2008, 07:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks