Results 1 to 5 of 5
  1. #1
    Ezrael is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Help with a program

    Hello everyone I need help with a certain program. Here is the question write a program to capture and validate a social security number to make sure it is inputted exactly in the following format.

    555-55-1234

    Use a loop that will ask for the user to enter the SSN. Then if it is invalid you will say it is invalid and using a do-while loop with a boolean flag make sure the user enters the correct form for SSN. if it is invalid it will keep going until you enter an SSN in valid format. In that case come out of the loop and display the valid SSN and exit program.

    This is what I have so far

    Java Code:
    public class SocialSecurity 
    {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            String inputString="";
            int X=-9;
            boolean flag=false;
     
             do
             {
                try
                  {
                  inputString=JOptionPane.showInputDialog("Enter your social security number: ");
                   X=Integer.parseInt(inputString);
                      if (X>0 && X<=10)
                       {
                       flag = true;
                        }
                        else
                       {
                       JOptionPane.showMessageDialog(null, "ERROR", "OUT OF RANGE" + inputString, JOptionPane.INFORMATION_MESSAGE);
                       flag=false;}
                  }
                  catch (NumberFormatException nef)
                 {
                     JOptionPane.showMessageDialog(null, nef, "Invalid" + inputString, JOptionPane.INFORMATION_MESSAGE);
                     flag=false;
                 }
        
             }while(!flag);
             
             System.out.print(X);
                System.exit(0);
            // TODO code application logic here
        }
    }
    If anyone can help it will be greatly appreciated.
    Last edited by pbrockway2; 03-17-2012 at 01:30 AM. Reason: code tags added

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

    Default Re: Help with a program

    Hi Ezrael, welcome to the forums!

    When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the formatting will be preserved when it appears here.

    -----

    So what is your problem?

    Does that code compile?

    Does it do what you intend or expect when you run it? If not, you have described the desired behaviour of the program, but perhaps you could also describe the behaviour of the code you posted.

  3. #3
    Ezrael is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with a program

    Quote Originally Posted by pbrockway2 View Post
    Hi Ezrael, welcome to the forums!

    When you post code, use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the formatting will be preserved when it appears here.

    -----

    So what is your problem?

    Does that code compile?

    Does it do what you intend or expect when you run it? If not, you have described the desired behaviour of the program, but perhaps you could also describe the behaviour of the code you posted.

    Whenever I type into the JOptionpane the social security number I always get a error message saying it is out of range. I have tried different things but can never get the SSN to be accepted. I dont know what is wrong with my code in that aspect. Thanks for the tips on how to post by the way

  4. #4
    Ezrael is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with a program

    java.lang.NumberFormatException: For input string:"xxx-xx-xxxx" is the message that I keep getting

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

    Default Re: Help with a program

    If you think about it "123-45-6789" cannot be parsed (interpreted) as an integer the same way "42" can be for example. And that's why you get the NumberFormatException.

    I haven't looked too closely, but the general logic of what have written looks OK. What is wrong is how you are setting flag. At the moment you are looking at the string the user enters and seeing if it represents an integer between 1 and 10. What you should do, I think, is look at each character of the string and check that each is correct. "correct" means the character should be a digit (or at particular places a hyphen). It is this check that should be used to set the value of flag.

    Perhaps you could write a separate method that validates a string in this way. Ie a method that takes a string and returns true if the first character is a digit, the second is a digit, the third is a digit, the fourth is a hyphen etc. And returns false otherwise. The advantage of writing a separate method for this is that it keeps your main() method from becoming too long and unreadable.

    -----

    The String and Character API documentation includes lots of methods that will be useful in validating the user's input.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Tags for this Thread

Posting Permissions

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