Java 如何使用此表格000-00-000输入

Java 如何使用此表格000-00-000输入,java,Java,问题 编写一个程序,在一行中读取学生姓名、社会保险号、用户ID和密码(用一个空格分隔)。该程序输出一个字符串,其中社会保险号的所有数字和密码中的所有字符均替换为x。(社会保险号的格式为000-00-000,用户ID和密码不包含任何空格,名称不包含任何数字。) 比如说 输入:用户输入:jimin 222-11-222 jimin 22 jim2000 说明:jimin是用户名-不应包含任何数字- 222-11-22是表“000-00-000”中的社会保险号 jimin22是用户id jim2000

问题 编写一个程序,在一行中读取学生姓名、社会保险号、用户ID和密码(用一个空格分隔)。该程序输出一个字符串,其中社会保险号的所有数字和密码中的所有字符均替换为x。(社会保险号的格式为000-00-000,用户ID和密码不包含任何空格,名称不包含任何数字。)

比如说 输入:用户输入:jimin 222-11-222 jimin 22 jim2000 说明:jimin是用户名-不应包含任何数字- 222-11-22是表“000-00-000”中的社会保险号 jimin22是用户id jim2000是用户密码

输出应该是 输出:jimin xxx xxx jimin 22 xxxxxxx

我不知道没有数字的名字是怎么写的
我不知道如何将社保号输出为“000-00-000”

扫描仪输入=新扫描仪(System.in);
字符串名称、So\n、ue\u id、密码;
int x;
////输入
name=input.next();
So_n=input.next();
ue_id=input.next();
密码=输入。下一步();
///输出
系统输出打印(名称+“\t”);
x=So_n.长度();

对于(int i=0;i替换SSO中的数字:

    x = So_n.length();
    for (int i = 0; i < x; i++) {
        if ('0' <= So_n.charAt(i) && So_n.charAt(i) <= '9') {
            System.out.print("x");
        } else {
            System.out.print(So_n.charAt(i));
        }
    }
x=So_n.length();
对于(int i=0;i如果('0'),问题的关键似乎是替换字符,而不是仅仅打印一些“X”字符。因此,处理替换的方法最合适

以下是一些可以解决OP问题的示例方法。可以轻松添加其他方法来验证用户名、密码(无论其复杂程度如何)等。没有提供在输入无效的情况下如何操作的详细信息,因此这里的一个案例仅显示一条消息

// for a non-null password, return a String of the same length with all
//   of the characters set to an 'X'
public static String getObfuscatedPassword(String pw)
{
    Objects.requireNonNull(pw, "Null pw input");
    return pw.replaceAll(".", "X");
}

// for a non-null ssn, return a String where every digit is replaced
//   by an 'X'; other characters (such as a -) are unchanged
public static String getObfuscatedSSN(String ssn)
{
    Objects.requireNonNull(ssn, "Null ssn input");

    return ssn.replaceAll("[0-9]", "X");
}

// return true if the specified ssn is not null and
//   matches the format of ###-##-###
public static boolean isValidSSN(String ssn)
{
    // ensures there is something for us to validate
    //   NOTE: technically the .isEmpty is not needed
    if (ssn == null || ssn.isEmpty()) {
        return false;
    }

    // ensure in the format of ###-##-###
    return Pattern.matches("[\\d]{3}-[\\d]{2}-[\\d]{3}", ssn);
}

// returns true if the username has at least one character, and
//  no digits; does not prevent having spaces in the name (could
//  be added); uses a for loop rather than regular expressions for
//  a fun difference in approach
public static boolean isValidUsername(String name)
{
    boolean charFound = false;
    boolean digitFound = false;
    if (name == null || name.isEmpty()) {
        return false;
    }

    for (int i = 0; i < name.length(); ++i) {
        // check if the current name[i] is a letter or digit
        //  the OR will keep a true once it is set
        charFound = charFound || Character.isLetter(name.charAt(i));
        digitFound = digitFound || Character.isDigit(name.charAt(i));
    }

    return charFound && ! digitFound;
}
样本输出:

使用更好的变量名将是一件好事


一些在线示例:

这很不清楚。当前代码有什么问题?聊天者是什么?我想在表单xxx xx xxx中用x替换社交号码,并向用户显示消息错误。而名称只是一个字符。请您为我们提供更多的输入和相应的输出?这真的很难理解现在就解决你的问题吧,也许数据会更清楚,我们可以帮助你。当社会保险号码一直是000-00-0000表格时,为什么不直接打印xxx xx xxx呢?@mayamar我已经考虑过了,但是如果用户插入的号码超过000-00-000,程序会发送错误的消息吗?不会运行,因为必须b表000-00-000中的e谢谢,我只有一个问题问我如何制作没有数字的字符串?@HalaHani,如果你的问题是如何检测字符串中的数字,我在回答中的
isValidUsername()
中提供了一个例子。如果你的问题是如何删除它们,那么
string noDigits=str.replaceAll(“[0-9]+”,”);
当用户写入带有数字的字符串时,我想对用户错误进行按摩,只需调整方法
isValidUsername(字符串)
根据需要。如果没有数字,它将返回true,如果有数字,它将返回false。通过检查,您可以给出一个错误输出。非常感谢,现在我只有一个问题,询问如何在第二个“-”之后确定用户必须输入3个数字
// for a non-null password, return a String of the same length with all
//   of the characters set to an 'X'
public static String getObfuscatedPassword(String pw)
{
    Objects.requireNonNull(pw, "Null pw input");
    return pw.replaceAll(".", "X");
}

// for a non-null ssn, return a String where every digit is replaced
//   by an 'X'; other characters (such as a -) are unchanged
public static String getObfuscatedSSN(String ssn)
{
    Objects.requireNonNull(ssn, "Null ssn input");

    return ssn.replaceAll("[0-9]", "X");
}

// return true if the specified ssn is not null and
//   matches the format of ###-##-###
public static boolean isValidSSN(String ssn)
{
    // ensures there is something for us to validate
    //   NOTE: technically the .isEmpty is not needed
    if (ssn == null || ssn.isEmpty()) {
        return false;
    }

    // ensure in the format of ###-##-###
    return Pattern.matches("[\\d]{3}-[\\d]{2}-[\\d]{3}", ssn);
}

// returns true if the username has at least one character, and
//  no digits; does not prevent having spaces in the name (could
//  be added); uses a for loop rather than regular expressions for
//  a fun difference in approach
public static boolean isValidUsername(String name)
{
    boolean charFound = false;
    boolean digitFound = false;
    if (name == null || name.isEmpty()) {
        return false;
    }

    for (int i = 0; i < name.length(); ++i) {
        // check if the current name[i] is a letter or digit
        //  the OR will keep a true once it is set
        charFound = charFound || Character.isLetter(name.charAt(i));
        digitFound = digitFound || Character.isDigit(name.charAt(i));
    }

    return charFound && ! digitFound;
}
public static void main (String[] args) throws java.lang.Exception
{
    String user = "jimin";
    String ssn = "222-11-222";
    String password = "WhatAWonderfulWorldItWouldBe";

    if (! isValidSSN(ssn)) {
        System.err.println("Invalid SSN");
    }

    String xdSsn = getObfuscatedSSN(ssn);

    String xdPw = getObfuscatedPassword(password);

    System.out.printf("%12s\t%s\t%s%n", user, xdSsn, xdPw);

}
  jimin   XXX-XX-XXX  XXXXXXXXXXXXXXXXXXXXXXXXXXXX