2

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;
}

}

  • Clearly you want help with your code. 1) Do you want someone to give you the solution? OR 2) do you want helpful pointers to assist you in solving it on your own? If you want 2, you should state that in your post, perhaps in BOLD to deter people from giving you the answer. – Scott Apr 30 '15 at 03:47
  • @scott thank you. I want pointers as opposed to the answers. I bolded it in an edit, as well as some changes to the code I've figured out. – Russell Culpepper Apr 30 '15 at 03:51
  • First I would suggest looking at validSSN(String s). You are passing in a string, say '123-45-6789'. You want a way to loop through this string a pull out each char. As you loop through each char, check each one in isDigit(char c) and isDash(char c). You should be able to google how to "loop through each char in string in java" to find your first answer. – Scott Apr 30 '15 at 03:56
  • You have nearly working code. You also have a serious bug. Test your code on this string "123-45--789" and you'll see it also passes. The reason is because you are only checking the first char and if it passes your logic, then return true. You never check the next char. So rethink your loop so that you can pull the return true out of the for loop. – Scott Apr 30 '15 at 05:46
  • I would also suggest debugging your code when you think it works. And try some simple cases where you know it should fail. Learning how to debug is invaluable. – Scott Apr 30 '15 at 05:48

2 Answers2

1

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]

Community
  • 1
  • 1
Scott
  • 6,089
  • 4
  • 34
  • 51
  • The last four lines of code you copied down are the actual numbers I'm using. The teacher wanted us to create a file on our desktop and read the SSN's from that file. Case 1 = 123-56-7890 Case 2 = 123-56-789 case 3 = 123-567-890 Case 4 = 123/56/7890 I made an edit to my code. It seems really long the way I did it, and I feel like I'm almost there. My problem now lies in my for loop when it crosses over s.charAt(3), it returns false becuase it does not comply with isDigit, which it's not supposed too. But it now makes the whole number return false. – Russell Culpepper Apr 30 '15 at 05:02
  • I got it! I'm sure there was a way easier way but if it works, if works. Thanks again. – Russell Culpepper Apr 30 '15 at 05:19
0

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.

comrench
  • 248
  • 2
  • 9