I simply have a program that must read a ssn (xxx-xx-xxxx) and determine if the given ssn is valid. I just need a way to check that the string only contains #'s and -'s.
import java.util.Scanner;
public class Prog7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a SSN:");
String ssn = input.next();
if((ssn.substring(0, 4).indexOf('-') != 3) || (ssn.substring(4, 7).indexOf('-') != 2) ||
(ssn.substring(ssn.length() - 5, ssn.length()).indexOf('-') != 0) ) {
System.out.println(ssn + " is an invalid social security number");
}
else {
System.out.println(ssn + " is a valid social security number");
}
}
}