java - 使用字符串验证 SSN

标签 java string validation io boolean-expression

我应该编写一个程序,将 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;
}

最佳答案

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

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

例如:

s = '123-45-6789'  

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

谷歌一下,看看你找到了什么。如果您没有看到有用的信息,请查看 this link .

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

Google“检查 char 是否为数字 java”。如果您没有找到任何东西,请参阅 link .测试 char 是否为破折号,'-' 应该很容易找到。

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

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

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

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

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

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

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

但我认为一旦您完成 1. 到 3. 工作,您就万事大吉了。

[编辑以解决您的错误:

剧透警告 如果您继续阅读,我会给出答案。因此,坚持下去,如果您放弃了,请查看下文并弄清楚为什么这样做有效而您的代码无效。

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

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

结束编辑]

关于java - 使用字符串验证 SSN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958414/

相关文章:

java - 与 log4j 输出的应用程序日志交互的最佳方式(同时也由应用程序更新)

string - 评估字符串中的 boolean 表达式 - Go

jQuery 单击验证而不提交

angularjs - Angular.js : Set form fields "dirty" upon submission

java - 朴素的继承问题 - Java

java - 在 Jersey REST 服务中调用 Google Geocode API 导致内部 400 错误

c++ - 用 C++ 编写文本/二进制文件最优雅的方法是什么?

C#字符串替换题

c# - Entity Framework 6 Model First 数据验证与 WPF

java - 传递JSP参数,然后检查它们是否存在