Java 使用字符串进行SSN验证

Java 使用字符串进行SSN验证,java,string,validation,io,boolean-expression,Java,String,Validation,Io,Boolean Expression,我应该编写一个程序,将SSN作为字符串读取,包括破折号,比如DDD-DD-dddddd,其中“D”是一个数字。如果有效,则打印“有效ssn”,如果无效,则打印相反的内容。我应该有四个方法:main、publicstaticbooleanvalidsn(strings)、publicsbooleanisdigit(charc)和publicsbooleanisdash(charc)。最后两个应该在方法public static boolean validsn(字符串s)中调用。我们目前正在研究字符

我应该编写一个程序,将SSN作为字符串读取,包括破折号,比如DDD-DD-dddddd,其中“D”是一个数字。如果有效,则打印“有效ssn”,如果无效,则打印相反的内容。我应该有四个方法:main、publicstaticbooleanvalidsn(strings)、publicsbooleanisdigit(charc)和publicsbooleanisdash(charc)。最后两个应该在方法public static boolean validsn(字符串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;
}
导入java.io.File;
导入java.util.Scanner;
公共类Lab14{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“C:\\Documents and Settings\\Russell\\Desktop\\Social Security Number.txt”);
扫描仪输入=新扫描仪(文件);
字符串case1=input.nextLine();
字符串case2=input.nextLine();
字符串case3=input.nextLine();
字符串case4=input.nextLine();
input.close();
System.out.println(“第一个案例是:“+case1+”。这是一个“+validsn(case1)+”条目”);
System.out.println(“第二种情况是:“+case2+”。这是一个“+validsn(case2)+”条目”);
System.out.println(“第三种情况是:“+case3+”。这是一个“+validsn(case3)+”条目”);
System.out.println(“第四种情况是:“+case4+”。这是一个“+validsn(case4)+”条目”);
}
公共静态布尔validSSN(字符串s){
如果(s.长度()==11){
if((isDash(s.charAt(3))==true)和&isDash(s.charAt(6))==true){
对于(int i=0;i
}

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

回答

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;
}
导入java.io.File;
导入java.util.Scanner;
公共类Lab14{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“C:\\Documents and Settings\\Russell\\Desktop\\Social Security Number.txt”);
扫描仪输入=新扫描仪(文件);
字符串case1=input.nextLine();
字符串case2=input.nextLine();
字符串case3=input.nextLine();
字符串case4=input.nextLine();
input.close();
System.out.println(“第一个案例是:“+case1+”。这是一个“+validsn(case1)+”条目”);
System.out.println(“第二种情况是:“+case2+”。这是一个“+validsn(case2)+”条目”);
System.out.println(“第三种情况是:“+case3+”。这是一个“+validsn(case3)+”条目”);
System.out.println(“第四种情况是:“+case4+”。这是一个“+validsn(case4)+”条目”);
}
公共静态布尔validSSN(字符串s){
如果(s.长度()==11){
对于(int i=0;i

}

验证破折号:

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");
        }
再次获取子字符串并将其全部添加。然后运行验证,确认这些是否为数字


如果您需要进一步的详细信息,请告诉我。

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

  • 正如我所评论的,您需要找到一种方法来循环传递给
    validsn(string s)
    的字符串中的每个字符
  • 例如:

    s = '123-45-6789'  
    
    您希望循环1,然后循环2,以此类推。每个字符都在字符串“s”中保留一个位置,从索引0到10

    谷歌一下,看看你发现了什么。如果你没有看到什么有用的东西,那就去看看

  • 当您循环遍历每个字符时,您希望测试每个字符,以确定它是一个数字(对于
    isDigit(char c)
    方法还是一个破折号(对于
    isDash(char c)
  • 谷歌“检查char是否为数字java”。如果你找不到任何东西,请看。测试字符是否是破折号,“-”应该很容易找到

    这应该考虑到
    isDigit(char c)
    isDash(char c)

  • 您需要在
    validsn(字符串s)
    中实现一些逻辑,以便在循环遍历每个字符时检查:
  • a) 如果在适当的索引处,字符是一个数字,否则返回false

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

    如果所有字符都通过了逻辑,则返回true

    你也有一些代码在你的主要,我不确定是怎么回事,即

    String case1 = input.nextLine();
    String case2 = input.nextLine();
    String case3 = input.nextLine();
    String case4 = input.nextLine();
    
    但我认为一旦你得到1。通过3。工作的时候你会准备好的

    [编辑以解决您的错误:

    剧透警报如果您继续阅读,我将给出答案。因此,请继续阅读,如果您放弃,请查看下面的内容,并找出为什么这样做有效而代码无效

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