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