Results 1 to 2 of 2
  1. #1
    jason99 is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Default Problem JOptionPane try catch

    import javax.swing.JOptionPane;

    public class trycatch
    {
    public static void main(String args[])
    {
    boolean loop=false;
    do
    {
    try
    {

    String strinput=JOptionPane.showInputDialog(null,"Enter a integer");
    int input=Integer.parseInt(strinput);

    }
    catch(NumberFormatException ex)
    {
    OptionPane.showMessageDialog(null,"Invalid value entered");
    loop=true;
    }


    }while(loop);
    }
    }

    can some one help me solve this , the problem is once the exception is catch then the CANCEL & CLOSED OPTION cant close the JOptionPane,thanks

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

    Default

    1) You'll want to set your loop variable to false at the beginning of the try block. Otherwise if you enter bad input once, you will loop forever, even if good input is given later.
    2) You'll want to check if the String returned from the showInputDialog method is null or not. Play with this and you'll figure it out.

    Best of luck.

Similar Threads

  1. [SOLVED] JOptionPane.ShowConfirmDialog() problem
    By jon80 in forum New To Java
    Replies: 15
    Last Post: 06-03-2009, 03:37 AM
  2. how to catch two exceptions in one catch()?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 11-06-2008, 11:54 AM
  3. JOptionPane problem
    By MomenT in forum New To Java
    Replies: 22
    Last Post: 10-02-2008, 06:56 PM
  4. [SOLVED] On Exit JoptionPane problem
    By Mir in forum New To Java
    Replies: 24
    Last Post: 07-15-2008, 08:39 AM
  5. Problem with JOptionPane.showInputDialog()
    By romina in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:33 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
  •