This discussion is archived
10 Replies Latest reply: Mar 1, 2013 3:08 AM by 994177 RSS

exception handler try..catch

896976 Newbie
Currently Being Moderated
I am new to Java, so please be patient, I am putting this exception in this method try..catch
but it is not working what I want to do is if user enter an alphanumeric character display message and try again!!!
public void actionPerformed(ActionEvent e) 
       {
              if (e.getSource() == buttons[0]) {
                myPanel.setBackground(Color.RED);
            } else if (e.getSource() == buttons[1]) {
                myPanel.setBackground(Color.BLUE);
            } else if (e.getSource() == buttons[2]) {
                myPanel.setBackground(Color.YELLOW);
            } else if (e.getSource() == buttons[3]) {
                myPanel.setBackground(Color.GREEN);
            } else if (e.getSource() == buttons[4]) {
               // String temp = JOptionPane.showInputDialog("New Scale");
                try {
                    String temp = JOptionPane.showInputDialog("New Scale");
                    myScale = Integer.parseInt(temp);
                    myPanel.repaint();
                 } catch (InputMismatchException InputMismatchException)
                 { System.err.println("Error: Invalid Input!");
                   JOptionPane.showMessageDialog(null, "Error: Invalid Input!");  
                   myScale = 40;
                 }
            }
    } //end of actionPerformed Method
//} // end buuton handler
Edited by: 893973 on Feb 21, 2013 12:02 PM
  • 1. Re: exception handler try..catch
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    so what exactly is your problem with the code?
    What does it (not) do?

    bye
    TPD
  • 2. Re: exception handler try..catch
    896976 Newbie
    Currently Being Moderated
    when the user enter a number it works when it enters a character, it fails

    at mysterypolygon.MysteryPolygon.actionPerformed(MysteryPolygon.java:91)
    myScale = Integer.parseInt(temp);
  • 3. Re: exception handler try..catch
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Unfortunately you did not post the previous line of the Error message, but I bet a penny that it does mention another exception then you are catching.
    Shouldn't that tell you anything?

    bye
    TPD
  • 4. Re: exception handler try..catch
    rp0428 Guru
    Currently Being Moderated
    >
    but it is not working what I want to do is if user enter an alphanumeric character display message and try again!!!
    >
    Doesn't that suggest to you that you need to use a 'loop' of some sort and only exit the loop when successful in either getting valid value or in getting a request to 'cancel'?
  • 5. Re: exception handler try..catch
    EJP Guru
    Currently Being Moderated
    It doesn't suggest that to me. This is a Swing GUI we are talking about. They are event-driven. No loops.
  • 6. Re: exception handler try..catch
    896976 Newbie
    Currently Being Moderated
    Hey guys, I was very clear in my post that I was new to JAVA, I see some arrogance in some of the answers, if you don't have anything positive to say, just don't.... our levels of skills are different... I am just learning... The name of the forum is New To Java...

    Thanks
  • 7. Re: exception handler try..catch
    EJP Guru
    Currently Being Moderated
    Instead of wasting time, or worse, being counterproductive, by assailing people who are trying to help you, how about you respond to the suggestions? What was the exception that you suppressed on top of the stack trace?
  • 8. Re: exception handler try..catch
    r035198x Pro
    Currently Being Moderated
    Your catch of InputMismatchException, is that from an old version of the code that was using a Scanner to get the input?
    Read the API specs for Integer.parseInt to see which exception to anticipate if the user doesn't enter valid values. And yes stacktraces are important if you are learning Java so read them fully when you get them.
  • 9. Re: exception handler try..catch
    rp0428 Guru
    Currently Being Moderated
    >
    It doesn't suggest that to me. This is a Swing GUI we are talking about. They are event-driven. No loops.
    >
    Well we may be interpreting what OP said differently.

    The original user action (for 'buttons[4]') IS event driven:
                } else if (e.getSource() == buttons[4]) {
                   // String temp = JOptionPane.showInputDialog("New Scale");
                    try {
                        String temp = JOptionPane.showInputDialog("New Scale");
                        myScale = Integer.parseInt(temp);
    And OP said this
    >
    what I want to do is if user enter an alphanumeric character display message and try again!!!
    >
    I interpret that to mean if the user presses 'buttons[4]' display a dialog AND KEEP DISPLAYING IT until then user enters a non-alphanumeric value.

    I do NOT interpret it to mean return to the user and make them press 'buttons[4]' again.

    Of course, for any functionality needed beyond simple display and capture the code should call an appropriate method.
  • 10. Re: exception handler try..catch
    994177 Newbie
    Currently Being Moderated
    Use Switch within a loop

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points