Russell Culpepper
Russell Culpepper

Reputation: 91

SSN validation using strings

I'm supposed to write a program that reads in a SSN as a string, including dashes, like DDD-DD-DDDD, where 'D' is a digit. If it's valid, it prints "valid ssn" and the opposite if it isn't valid. I'm supposed to have four methods: main, public static boolean validSSN(String s), public boolean isDigit(char c), and public boolean isDash(char c). The last two are supposed to be invoked in the method public static boolean validSSN(String s). We are currently going over strings and text I/O. I know how to read the strings using java.io.File, but beyond that, I'm at a loss. Here is what I have so far:

import java.io.File;
import java.util.Scanner;

public class Lab14 {
public static void main(String[] args)throws Exception {
    File file = new File("C:\\Documents and     Settings\\Russell\\Desktop\\Social-Security-Numbers.txt");
    Scanner input = new Scanner(file);
        String case1 = input.nextLine();
        String case2 = input.nextLine();
        String case3 = input.nextLine();
        String case4 = input.nextLine();
    input.close();
    System.out.println("The first case is: " + case1 + ". This is a " + validSSN(case1) + " entry.");
    System.out.println("The second case is: " + case2 + ". This is a " + validSSN(case2) + " entry.");
    System.out.println("The third case is: " + case3 + ". This is a " + validSSN(case3) + " entry.");
    System.out.println("The fourth case is: " + case4 + ". This is a " + validSSN(case4) + " entry.");
}
public static boolean validSSN(String s){
    if (s.length() == 11){
        if((isDash(s.charAt(3))==true) && isDash(s.charAt(6))==true){
            for(int i = 0; i < s.length(); i++){
                char c = s.charAt(i);
                if(isDigit(s.charAt(i))==false)
                    return false;
            }
        }
        return true;
    }
    else
        return false;
}
public static boolean isDigit(char c){
    if((c == '0') || (c == '1') || (c == '2') || (c == '3') || 
    (c == '4') || (c == '5') || (c == '6') || (c == '7') || 
    (c == '8') || (c == '9'))
        return true;
    else
        return false;

}
public static boolean isDash(char c){
    if(c == '-')
        return true;
    else
        return false;
}

}

I prefer help as opposed to the answer since I am just now learning how to code.

ANSWER

import java.io.File;
import java.util.Scanner;

public class Lab14 {
public static void main(String[] args)throws Exception {
    File file = new File("C:\\Documents and Settings\\Russell\\Desktop\\Social-Security-Numbers.txt");
    Scanner input = new Scanner(file);
        String case1 = input.nextLine();
        String case2 = input.nextLine();
        String case3 = input.nextLine();
        String case4 = input.nextLine();
    input.close();
    System.out.println("The first case is: " + case1 + ". This is a " + validSSN(case1) + " entry.");
    System.out.println("The second case is: " + case2 + ". This is a " + validSSN(case2) + " entry.");
    System.out.println("The third case is: " + case3 + ". This is a " + validSSN(case3) + " entry.");
    System.out.println("The fourth case is: " + case4 + ". This is a " + validSSN(case4) + " entry.");
}
public static boolean validSSN(String s){
    if (s.length() == 11){
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if(isDigit(s.charAt(i))==true && ((isDash(s.charAt(3))==true) && isDash(s.charAt(6))==true)){
                    return true;
        }
        else
            return false;
        }
        return false;
}
    else return false;
}
public static boolean isDigit(char c){
    if((c == '0') || (c == '1') || (c == '2') || (c == '3') || 
    (c == '4') || (c == '5') || (c == '6') || (c == '7') || 
    (c == '8') || (c == '9'))
        return true;
    else
        return false;
}
public static boolean isDash(char c){
    if(c == '-')
        return true;
    else
        return false;
}

}

Upvotes: 2

Views: 2394

Answers (2)

Scott
Scott

Reputation: 6389

I'll sketch out what I think you can easily tackle with some logic and googling.

  1. As I commented, you need to find a way to loop through each char in the string you're passing to validSSN(String s),

For example:

s = '123-45-6789'  

You want to loop through 1, then 2, etc. Each of these chars hold a position in the string 's', starting from index 0 through 10.

Google it, see what you find. If you're not seeing something helpful check out this link.

  1. As you loop through each char you want to test each one to know if it is a number, for your isDigit(char c) method, or a dash, for isDash(char c).

Google "check if char is number java". If you don't find anything see link. Testing if char is a dash, '-' should be easy to find.

This should take care of isDigit(char c) and isDash(char c).

  1. You need to implement some logic in validSSN(String s) so that as you loop through each char, you check:

a) if at the appropriate index the char is a digit, otherwise return false

b) if at the appropriate index the char is a dash, otherwise return false

If all char pass your logic, then return true.

You also have some code in your main that I'm unsure what is going on, namely,

String case1 = input.nextLine();
String case2 = input.nextLine();
String case3 = input.nextLine();
String case4 = input.nextLine();

But I think once you get 1. through 3. working you'll be all set.

[EDIT to address your bug:

SPOILER ALERT If you keeping reading I am giving away the answer. So keep at it and if you give up, look below and figure out why this works and your code doesn't.

So for if-else logic it is my preference to test if something is false first, there may be several cases, so when you get through all of them, then return true. There may be better ways to code the following, but I think this is understandable:

public static boolean validSSN(String s){
    // don't bother doing anything else if the length is wrong
    if (s.length() != 11) { return false; } 
    else {
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i); 
            // now that you have c, use it. Don't do s.charAt(i) again.

            // if index is both {not 3 AND not 6} do...
            if ((i != 3) && (i != 6)) {

                // you don't need to check "isDigit(s.charAt(i))==false"
                if (!isDigit(c)) { return false; } // if not numeric, return false
            }
            // here we are either index i=3 OR i=6, so if c is not a dash return false
            else if (!isDash(c)) { return false; }
        }
        // at this point we exhausted our loop and couldn't 
        // find anything false, so return true
        return true;        
    }
}    

END EDIT]

Upvotes: 1

comrench
comrench

Reputation: 248

For validating the dashes:

StringBuilder caseStringBuilder = new StringBuilder(String.valueOf("346-45-3456"));
if(caseStringBuilder.substring(3,4).equalsIgnoreCase("-") && caseStringBuilder.substring(6,7).equalsIgnoreCase("-")){
    System.out.println("Dashes validated successfully");
}else{
    System.out.println("Dash validatioin failed");
} 

Just fetched the substring at the location of dashes and compared.

For checking the numeric fields:

StringBuilder caseStringBuilder = new StringBuilder(String.valueOf("346-45-3456"));
if((caseStringBuilder.substring(0,3)+caseStringBuilder.substring(4,6)+caseStringBuilder.substring(7,11)).matches(".*\\d.*")){
            System.out.println("It contains only numbers");
        }

Again fetched the substrings and added them all. Then ran the validation that those are numbers or not.

Let me know if you need any further details on this.

Upvotes: 0

Related Questions