使用字符串验证 SSN

SSN validation using strings

我应该编写一个程序,将 SSN 作为字符串读取,包括破折号,例如 DDD-DD-DDDD,其中 'D' 是一个数字。如果有效,则打印 "valid ssn",如果无效,则相反。我应该有四种方法:main、public static boolean validSSN(String s)、public boolean isDigit(char c) 和 public boolean isDash(char c)。最后两个应该在方法 public static boolean validSSN(String s) 中调用。我们目前正在处理字符串和文本 I/O。我知道如何使用 java.io.File 读取字符串,但除此之外,我不知所措。这是我目前所拥有的:

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

}

我更喜欢帮助而不是答案,因为我刚刚开始学习如何编码。

回答

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

}

为了验证破折号:

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

刚刚提取破折号位置的子串并进行比较。

用于检查数字字段:

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

再次获取子字符串并将它们全部添加。然后 运行 验证这些是不是数字。

如果您需要更多详细信息,请告诉我。

我会勾勒出我认为您可以通过一些逻辑和谷歌搜索轻松解决的问题。

  1. 正如我评论的那样,您需要找到一种方法来遍历要传递给 validSSN(String s)
  2. 的字符串中的每个字符

例如:

s = '123-45-6789'  

您想遍历 1,然后 2,等等。这些字符中的每一个都在字符串 's' 中占据一个位置,从索引 0 到 10。

Google它,看看你发现了什么。如果您没有看到有用的信息,请查看 this link.

  1. 当您遍历每个字符时,您想要测试每个字符以了解它是否是一个数字,对于您的 isDigit(char c) 方法,或者一个破折号,对于 isDash(char c).

Google"check if char is number java"。如果您没有找到任何内容,请参阅 link。测试 char 是否为破折号,'-' 应该很容易找到。

这应该处理 isDigit(char c)isDash(char c)

  1. 您需要在 validSSN(String s) 中实现一些逻辑,以便在遍历每个字符时检查:

a) 如果在适当的索引处字符是数字,否则 return false

b) 如果在适当的索引处字符是破折号,否则 return false

如果所有字符都通过您的逻辑,则 return 为真。

您的主程序中还有一些代码,我不确定发生了什么,即

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

但我认为一旦你完成 1. 到 3. 工作,你就会准备就绪。

[编辑 以解决您的错误:

剧透警告 如果你继续阅读,我会给出答案。所以坚持下去,如果你放弃了,请看下面并弄清楚为什么这行得通而你的代码行不通。

所以对于 if-else 逻辑,我更喜欢先测试某件事是否为假,可能有几种情况,所以当你通过所有这些时,然后 return 为真。可能有更好的方法来编写以下代码,但我认为这是可以理解的:

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

编辑结束]