java 验证输入字符串是否是正确的社会安全号码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29764731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:54:01  来源:igfitidea点击:

Verifying if an input string is a correct Social Security Number

java

提问by lawtonhself

Here is the Exact Instructions, but I have already done some of it. I will Write what I need help with at the bottom.

这是确切的说明,但我已经做了一些。我会在底部写下我需要帮助的内容。

.I want to Write a program that reads in a social security number as a string (including the dashes) in the format DDD-DD-DDDD where D is a digit.

.我想编写一个程序,以 DDD-DD-DDDD 格式将社会保险号作为字符串(包括破折号)读入,其中 D 是数字。

.I need to Write a method: public static boolean validSSN(String s)to check whether a string is in the format of DDD-DD-DDDD. Before I do anything I have to check the length of the input string.

.我需要写一个方法:public static boolean validSSN(String s)来检查一个字符串是否是DDD-DD-DDDD格式。在我做任何事情之前,我必须检查输入字符串的长度。

.It should be 11 characters long(including the -'s) and anything else is invalid.

.它应该是 11 个字符长(包括 - 的),其他任何内容都是无效的。

.I need to Write 2 boolean methods: isDigit(char c) and isDash(char c) to help check the format of the input and invoke them in the method body of validSSN.

.我需要编写 2 个布尔方法:isDigit(char c) 和 isDash(char c) 来帮助检查输入的格式并在 validSSN 的方法体中调用它们。

.In the main method, I need to use a scanner object's nextLine() method to read an entire line as a string invoke the validSSN method print the input and whether or not it is valid.

.在 main 方法中,我需要使用扫描仪对象的 nextLine() 方法将整行作为字符串读取,调用 validSSN 方法打印输入以及它是否有效。

Here is what I have so far:

这是我到目前为止所拥有的:

import java.util.Scanner;

public class lab14 {

    public static void main(String[] args){
        Scanner SSN = new Scanner(System.in);

        System.out.println("Enter a Social Security Number");
        int num = SSN.nextInt();

    }
    public static boolean validSSN(String s){
        if 

    }
    public static boolean isDigit(char c){

    }
    public static boolean isDash(char c){

    }

}

So, I have written out the header for the main method, and the header for the boolean method that checks to see if there are 11 characters, a header for a boolean method that checks to see if the numbers are in the right place, and a header for a boolean method that checks to see if the dashes are in the right place. I have imported a Scanner method, and sent out for the user ti imput a social security number.

所以,我写出了 main 方法的标题,以及检查是否有 11 个字符的布尔方法的标题,检查数字是否在正确位置的布尔方法的标题,以及用于检查破折号是否在正确位置的布尔方法的标头。我已经导入了一个 Scanner 方法,并发送给用户输入社会安全号码。

What I need help with is what to put in the body of each method. I need help to return true if there is 11 digits, if the numbers are in the right place, and if the dashes are in the right place. So If I were to input 123-56-7890, It would say that "123-56-7890 is a valid SSN", and I were to input 123/56/7890 or 123-567-890 it would say that ""123-567-890" or "123-567-890" is not a SSN". Can someone help me with verifying that the input is a SSN.

我需要帮助的是在每个方法的主体中放入什么。如果有 11 位数字,如果数字在正确的位置,并且破折号在正确的位置,我需要帮助返回 true。所以如果我输入 123-56-7890,它会说“123-56-7890 是一个有效的 SSN”,而我输入 123/56/7890 或 123-567-890 它会说“” 123-567-890" 或 "123-567-890" 不是 SSN"。有人可以帮我验证输入是否是 SSN。

回答by TheLostMind

Use regex like this :

像这样使用正则表达式:

myString.matches("\d{3}-\d{2}-\d{4}");

回答by user8486398

ssn.matches("^(\d{3}-?\d{2}-?\d{4})$");

回答by Daniel Nugent

Of course using a regex is the way to go, but given that this is an assignment and it sounds like they want you to do it manually, this is probably what they're looking for.

当然,使用正则表达式是可行的方法,但鉴于这是一项任务,而且听起来他们希望您手动完成,这可能就是他们正在寻找的。

Read this documentationon Character.isDigit()

阅读本文件Character.isDigit()

Read this documentationon String.charAt()

阅读本文件String.charAt()

Read about the conditional operator here

在此处阅读有关条件运算符的信息

import java.util.Scanner;

public class lab14 {

    public static void main(String[] args){
        Scanner SSN = new Scanner(System.in);
        String s = null;
        boolean valid = false;

        System.out.println("Enter a Social Security Number");

        while (valid == false){
          try{
            s = SSN.nextLine();
            valid = true;
          } catch(Exception e){
            System.out.println("No input! Enter a Social Security Number");
          }
        }

        String result = (validSSN(s) ? " is a valid SSN" : " is not a SSN");
        System.out.println(s + result);

    }

    public static boolean validSSN(String str){
      //check length first
      if (str.length() != 11) return false;

      //traverse through each character
      for (int i = 0; i < str.length(); i++){
        if (i <= 2){
          //these must be digits, otherwise return false
          if (!isDigit(str.charAt(i))){
            return false;
          }
        }
        else if (i == 3 || i == 6){
          //these must be dashes, otherwise return false
          if (!isDash(str.charAt(i))){
            return false; 
          }
        }
        else if (i > 6){
          //these must be digits, otherwise return false
          if (!isDigit(str.charAt(i))){
            return false;
          }
        }
      }

      //return true if it didn't error out
      return true;
    }

    public static boolean isDigit(char c){
      return Character.isDigit(c);
    }

    public static boolean isDash(char c){
      return (c == '-');
    }

}