Data Validation

Discussion in 'Java' started by BlackJackal, Jan 29, 2007.

  1. BlackJackal

    BlackJackal Guest

    I am really new to programming so take it easy on me :)

    Here is the code. Basically what I want it to do is whenever the user
    enters in 1 2 or 3 to say good job. When the user enters 4 to quit
    and anything else display an error message. This code works fine as
    long as the user enters in a int if anything else is entered it
    crashes. How can i fix this? Thanks

    import javax.swing.*;
    public class Input123
    {
    public static void main(String[] args)
    {
    String input;
    int selection = 0;
    while (selection != 4) {
    input = JOptionPane.showInputDialog(null,"Enter in either
    1, 2, 3, or 4 to quit");
    selection = Integer.parseInt(input);
    if(selection == 1 || selection == 2 || selection == 3) {
    JOptionPane.showMessageDialog(null,"Good Job");
    }
    else if(selection == 4){
    System.exit(0);
    }
    else {
    JOptionPane.showMessageDialog(null,"You have entered an
    invalid character, Please try again.");
    }

    }

    System.exit(0);
    }
    }
     
    BlackJackal, Jan 29, 2007
    #1
    1. Advertising

  2. BlackJackal

    Eric Sosman Guest

    BlackJackal wrote On 01/29/07 17:44,:
    > I am really new to programming so take it easy on me :)
    >
    > Here is the code. Basically what I want it to do is whenever the user
    > enters in 1 2 or 3 to say good job. When the user enters 4 to quit
    > and anything else display an error message. This code works fine as
    > long as the user enters in a int if anything else is entered it
    > crashes. How can i fix this? Thanks
    >
    > import javax.swing.*;
    > public class Input123
    > {
    > public static void main(String[] args)
    > {
    > String input;
    > int selection = 0;
    > while (selection != 4) {
    > input = JOptionPane.showInputDialog(null,"Enter in either
    > 1, 2, 3, or 4 to quit");
    > selection = Integer.parseInt(input);
    > if(selection == 1 || selection == 2 || selection == 3) {
    > JOptionPane.showMessageDialog(null,"Good Job");
    > }
    > else if(selection == 4){
    > System.exit(0);
    > }
    > else {
    > JOptionPane.showMessageDialog(null,"You have entered an
    > invalid character, Please try again.");
    > }
    >
    > }
    >
    > System.exit(0);
    > }
    > }


    If the user enters junk, Integer.parseInt() will throw
    NumberFormatException. If you want to keep going after the
    exception is thrown, you must catch it and deal with it.
    Here's one way:

    while (true) { // selection != 4 is useless
    input = ...;
    try {
    selection = Integer.parseInt(input);
    }
    catch (NumberFormatException ex) {
    selection = 0;
    }
    if (... as above ...)
    }

    --
     
    Eric Sosman, Jan 29, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Mackay
    Replies:
    0
    Views:
    2,703
    Colin Mackay
    Jun 25, 2003
  2. Libs
    Replies:
    0
    Views:
    1,524
  3. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Tommy
    Nov 29, 2003
  4. Matt
    Replies:
    14
    Views:
    4,144
    Chad Z. Hower aka Kudzu
    Jan 30, 2004
  5. Lucas Tam
    Replies:
    2
    Views:
    1,127
    Lucas Tam
    Feb 26, 2004
Loading...

Share This Page