10 Replies - 355 Views - Last Post: 25 October 2019 - 10:45 AM Rate Topic: -----

#1 smittie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-October 19

Checking a social security input

Posted 25 October 2019 - 12:16 AM

My goal is to write a program that prompts a user to enter a social security number and then it checks to make sure it's actually a valid number. My problem is that it always returns valid as long as 11 characters of any type are entered when it should reply false. I've tried out a bunch of different combinations but nothing seems to fix this issue. I have no idea what is failing. Any help is greatly appreciated.

import java.util.Scanner;

public class SSN {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);
		
		System.out.println("Enter a SSN with hyphens");
		
		String social = in.nextLine();
		
		if (
        ( (social.length() == 11) &&
        Character.isDigit(social.charAt(0)) && 
        Character.isDigit(social.charAt(1)) &&  
        Character.isDigit(social.charAt(2)) &&  
        Character.isDigit(social.charAt(4)) &&
        Character.isDigit(social.charAt(5)) &&
        Character.isDigit(social.charAt(7)) &&
        Character.isDigit(social.charAt(8)) &&
        Character.isDigit(social.charAt(9)) &&
        Character.isDigit(social.charAt(10)) && 
        (social.charAt(3) == '-') &&  
        (social.charAt(6) == '-')))
		
		System.out.println(social + " is a valid Social Security Number");

		else

		System.out.println(social + " is an invalid Social Security Number");
}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Checking a social security input

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: Checking a social security input

Posted 25 October 2019 - 03:39 AM

Can you copy and post here the contents of the console window for some of the tests you have done that show what you are talking about?

Note: It is better practice to enclose the code controlled by an if statement inside of {}s
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 3038
  • View blog
  • Posts: 11,669
  • Joined: 03-December 12

Re: Checking a social security input

Posted 25 October 2019 - 06:13 AM

Why check each number individually?

class Validation
{
    public static boolean tryParseInt(String value) 
    {  
        try 
        {  
            Integer.parseInt(value);  
            return true;  
        } catch (NumberFormatException e) {  
            return false;  
        }  
    }
	
    public static boolean socialSecurityNumber(String id)
    {
        String num = id.replace("-", "");
	if(num.length() != 9)
	    return false;
	if(!tryParseInt(num))
	    return false;
	return true;
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 smittie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-October 19

Re: Checking a social security input

Posted 25 October 2019 - 09:39 AM

View Postastonecipher, on 25 October 2019 - 06:13 AM, said:

Why check each number individually?

class Validation
{
    public static boolean tryParseInt(String value) 
    {  
        try 
        {  
            Integer.parseInt(value);  
            return true;  
        } catch (NumberFormatException e) {  
            return false;  
        }  
    }
	
    public static boolean socialSecurityNumber(String id)
    {
        String num = id.replace("-", "");
	if(num.length() != 9)
	    return false;
	if(!tryParseInt(num))
	    return false;
	return true;
    }
}



I'm just learning the very basics so I'd like to figure out why the code I've written isn't working rather than a better way to do it at the moment. I can't figure out what exactly is going wrong and that is what I'd really like to know.
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3648
  • View blog
  • Posts: 16,768
  • Joined: 20-September 08

Re: Checking a social security input

Posted 25 October 2019 - 09:45 AM

Have to say the last code would pass the input were the hyphens wrongly placed ;) Even two hyphens followed by 9 digits would pass

This post has been edited by g00se: 25 October 2019 - 09:45 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: Checking a social security input

Posted 25 October 2019 - 09:47 AM

Quote

why the code I've written isn't working


Can you copy and post here the contents of the console window for some of the tests you have done that show what you are talking about?
Was This Post Helpful? 0
  • +
  • -

#7 smittie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-October 19

Re: Checking a social security input

Posted 25 October 2019 - 10:19 AM

View PostNormR, on 25 October 2019 - 09:47 AM, said:

Quote

why the code I've written isn't working


Can you copy and post here the contents of the console window for some of the tests you have done that show what you are talking about?


Sorry wasn't at the computer until now.

Enter a SSN with hyphens
aaaaaaaaaaa
aaaaaaaaaaa is a valid Social Security Number

Enter a SSN with hyphens
123-45-6789
123-45-6789 is a valid Social Security Number

Enter a SSN with hyphens
asd-zx-qwer
asd-zx-qwer is a valid Social Security Number

Enter a SSN with hyphens
12345678912
12345678912 is a valid Social Security Number

Enter a SSN with hyphens
abcdefghijkl
abcdefghijkl is an invalid Social Security Number

Enter a SSN with hyphens
123456
123456 is an invalid Social Security Number

Enter a SSN with hyphens
-----------
----------- is a valid Social Security Number
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: Checking a social security input

Posted 25 October 2019 - 10:23 AM

Please post your current code. The posted code (#1) works for me - it gives an invalid message:

Quote

Enter a SSN with hyphens
aaaaaaaaaaa is an invalid Social Security Number


Or copy the posted code and use that for testing instead of what you have now.

This post has been edited by NormR: 25 October 2019 - 10:25 AM

Was This Post Helpful? 1
  • +
  • -

#9 smittie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-October 19

Re: Checking a social security input

Posted 25 October 2019 - 10:27 AM

Current Code

import java.util.Scanner;

public class SSN {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);
		
		System.out.println("Enter a SSN with hyphens");
		
		String social = in.nextLine();		
		
		if (
        (social.length() == 11) &&
        Character.isDigit(social.charAt(0)) && 
        Character.isDigit(social.charAt(1)) &&  
        Character.isDigit(social.charAt(2)) &&  
        Character.isDigit(social.charAt(4)) &&
        Character.isDigit(social.charAt(5)) &&
        Character.isDigit(social.charAt(7)) &&
        Character.isDigit(social.charAt(8)) &&
        Character.isDigit(social.charAt(9)) &&
        Character.isDigit(social.charAt(10)) && 
        (social.charAt(3) == '-') &&  
        (social.charAt(6) == '-'))
		
		System.out.println(social + " is a valid Social Security Number");

		else

		System.out.println(social + " is an invalid Social Security Number");
}
}

Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 6,062
  • Joined: 25-December 13

Re: Checking a social security input

Posted 25 October 2019 - 10:31 AM

That code works as expected for me.

Quote

aaaaaaaaaaa is an invalid Social Security Number

123-45-6789 is a valid Social Security Number


Make sure you are using that code and not an earlier version.

This post has been edited by NormR: 25 October 2019 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#11 smittie   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 18-October 19

Re: Checking a social security input

Posted 25 October 2019 - 10:45 AM

Thanks to NormR I think I figured out what was wrong, maybe. I opened multiple project folders to test out different versions of code I wrote up, but on this one specifically I didn't change the class name. Once I replaced the original code with this one it started working correctly. I didn't realize that using the same class name in different projects still connected them. Or my plumber brain is just full of *hit.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1