Reputation: 41
Here is the Exact Instructions, but I have already done some of it. I will Write what I need help with at the bottom.
.I want to Write a program that reads in a social security number as a string (including the dashes) in the format DDD-DD-DDDD where D is a digit.
.I need to Write a method: public static boolean validSSN(String s)to check whether a string is in the format of DDD-DD-DDDD. Before I do anything I have to check the length of the input string.
.It should be 11 characters long(including the -'s) and anything else is invalid.
.I need to Write 2 boolean methods: isDigit(char c) and isDash(char c) to help check the format of the input and invoke them in the method body of validSSN.
.In the main method, I need to use a scanner object’s nextLine() method to read an entire line as a string invoke the validSSN method print the input and whether or not it is valid.
Here is what I have so far:
import java.util.Scanner;
public class lab14 {
public static void main(String[] args){
Scanner SSN = new Scanner(System.in);
System.out.println("Enter a Social Security Number");
int num = SSN.nextInt();
}
public static boolean validSSN(String s){
if
}
public static boolean isDigit(char c){
}
public static boolean isDash(char c){
}
}
So, I have written out the header for the main method, and the header for the boolean method that checks to see if there are 11 characters, a header for a boolean method that checks to see if the numbers are in the right place, and a header for a boolean method that checks to see if the dashes are in the right place. I have imported a Scanner method, and sent out for the user ti imput a social security number.
What I need help with is what to put in the body of each method. I need help to return true if there is 11 digits, if the numbers are in the right place, and if the dashes are in the right place. So If I were to input 123-56-7890, It would say that "123-56-7890 is a valid SSN", and I were to input 123/56/7890 or 123-567-890 it would say that ""123-567-890" or "123-567-890" is not a SSN". Can someone help me with verifying that the input is a SSN.
Upvotes: 1
Views: 15307
Reputation: 43322
Of course using a regex is the way to go, but given that this is an assignment and it sounds like they want you to do it manually, this is probably what they're looking for.
Read this documentation on Character.isDigit()
Read this documentation on String.charAt()
Read about the conditional operator here
import java.util.Scanner;
public class lab14 {
public static void main(String[] args){
Scanner SSN = new Scanner(System.in);
String s = null;
boolean valid = false;
System.out.println("Enter a Social Security Number");
while (valid == false){
try{
s = SSN.nextLine();
valid = true;
} catch(Exception e){
System.out.println("No input! Enter a Social Security Number");
}
}
String result = (validSSN(s) ? " is a valid SSN" : " is not a SSN");
System.out.println(s + result);
}
public static boolean validSSN(String str){
//check length first
if (str.length() != 11) return false;
//traverse through each character
for (int i = 0; i < str.length(); i++){
if (i <= 2){
//these must be digits, otherwise return false
if (!isDigit(str.charAt(i))){
return false;
}
}
else if (i == 3 || i == 6){
//these must be dashes, otherwise return false
if (!isDash(str.charAt(i))){
return false;
}
}
else if (i > 6){
//these must be digits, otherwise return false
if (!isDigit(str.charAt(i))){
return false;
}
}
}
//return true if it didn't error out
return true;
}
public static boolean isDigit(char c){
return Character.isDigit(c);
}
public static boolean isDash(char c){
return (c == '-');
}
}
Upvotes: 0
Reputation: 36304
Use regex like this :
myString.matches("\\d{3}-\\d{2}-\\d{4}");
Upvotes: 6