【问题标题】:SSN validation using strings使用字符串的 SSN 验证
【发布时间】:2015-04-30 03:17:03
【问题描述】:

我应该编写一个程序,将 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)你想要有人给你解决方案吗?或者 2) 你想要有用的指针来帮助你自己解决它吗?如果你想要 2,你应该在你的帖子中声明,也许用粗体来阻止人们给你答案。
  • @scott 谢谢。我想要指针而不是答案。我在编辑中将其加粗,并对我想出的代码进行了一些更改。
  • 首先我建议查看validSSN(String s)。您正在传递一个字符串,例如“123-45-6789”。您想要一种方法来循环遍历此字符串并拉出每个字符。当您遍历每个字符时,检查 isDigit(char c) 和 isDash(char c) 中的每个字符。你应该能够谷歌如何“遍历java中字符串中的每个字符”来找到你的第一个答案。
  • 您的代码几乎可以工作。你也有一个严重的错误。在这个字符串“123-45--789”上测试你的代码,你会看到它也通过了。原因是您只检查第一个字符,如果它通过了您的逻辑,则返回 true。你永远不会检查下一个字符。所以重新考虑你的循环,这样你就可以将 return true 拉出 for 循环。
  • 我还建议在您认为代码可行时调试您的代码。并尝试一些您知道它应该失败的简单案例。学习如何调试是非常宝贵的。

标签: java string validation io boolean-expression


【解决方案1】:

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

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

例如:

s = '123-45-6789'  

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

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

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

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

这应该注意isDigit(char c)isDash(char c)

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

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

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

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

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

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

但我认为,一旦你完成了 1. 到 3. 的工作,你就会万事俱备。

[编辑以解决您的错误:

剧透警告如果您继续阅读,我将给出答案。所以坚持下去,如果你放弃了,看看下面,找出为什么它有效而你的代码无效。

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

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

结束编辑]

【讨论】:

  • 您复制的最后四行代码是我使用的实际数字。老师希望我们在桌面上创建一个文件并从该文件中读取 SSN。案例 1 = 123-56-7890 案例 2 = 123-56-789 案例 3 = 123-567-890 案例 4 = 123/56/7890 我对我的代码进行了编辑。我这样做的方式似乎真的很长,我觉得我快到了。我的问题现在出在我的 for 循环中,当它越过 s.charAt(3) 时,它返回 false,因为它不符合 isDigit,这也不应该。但它现在使整数返回 false。
  • 我明白了!我确信有一种更简单的方法,但如果它有效,如果有效。再次感谢。
【解决方案2】:

用于验证破折号:

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

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

如果您需要任何关于此的更多详细信息,请告诉我。

【讨论】:

  • 我们根本没有使用过 StringBuilder。不过感谢您的意见!
猜你喜欢
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
相关资源
最近更新 更多