Results 1 to 13 of 13
  1. #1
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Post 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    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?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The JOptionPane will return a String. You might convert it to an int with the Integer method parseInt().

  5. #5
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    I am reading in textbook about JOptionPane Option Dialog. I will look into parseInt as well.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    showOptionDialog() is probably the neatest way of coding it - but it's also more complex than a simple input dialog.

  7. #7
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    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 :)

  8. #8
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    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.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    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. #10
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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)
    
    	}

  11. #11
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

    Default

    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?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,458
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bentho View Post
    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?
    I would've expected something like this in your switch:

    Java Code:
    switch (userSelection)
    {
    case 0: Benjamin.last();
    break;
    case 1: Benjamin.middle();
    break;
    case 2: Benjamin.first();
    break; 
    }
    Pay close attention to the case numbers and the methods being called.

    kind regards,

    Jos

  13. #13
    bentho is offline Member
    Join Date
    Oct 2010
    Posts
    13
    Rep Power
    0

Similar Threads

  1. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  2. switch on a string
    By dinosoep in forum New To Java
    Replies: 14
    Last Post: 02-16-2010, 10:31 AM
  3. switch
    By dj kourampies in forum New To Java
    Replies: 17
    Last Post: 01-30-2009, 05:32 PM
  4. switch
    By dj kourampies in forum New To Java
    Replies: 2
    Last Post: 01-30-2009, 08:46 AM
  5. Switch help please!!!!
    By soc86 in forum New To Java
    Replies: 6
    Last Post: 11-23-2008, 07:25 PM

Posting Permissions

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