首页 > 解决方案 > 将超过 4 个字符的字符串表示为 4 个字符

问题描述

public class CreditCardNumber {
private String issuerId;
private String accountNum;
private int checkDigit = 9;
private StringBuilder builder;

public CreditCardNumber(String id, String accNum) {
    this();
    if (id != null && accNum != null && id.length() == 6 && accNum.length() == 9 && isDigit(id) == true
            && isDigit(accNum) == true) {
        accountNum = accNum;
        issuerId = id;
    }
    setCheckDigit();
}

public CreditCardNumber() {
    issuerId = "000000";
    accountNum = "999999999";
}

public String getId() {
    return issuerId;
}

public String getAccNum() {
    return accountNum;
}

public int getCheckDigit() {
    return checkDigit;
}

// A
private void setCheckDigit() {
    int sum = checkSum();
    int temp = sum + checkDigit;
        if(temp%10 != 0) {
            int num = temp%10;
            checkDigit = checkDigit - num;
        }
}

// Method to check if each character in string is a digit
public boolean isDigit(String s) {
    boolean condition = true;
    for (int i = 0; i < s.length(); i++) {
        if (Character.isDigit(s.charAt(i))) {
            condition = true;
        }
    }
    return true;
}

// B
public void changeId(String id) {
    int max = 9;
    int min = 0;

    if (id != null && id.length() == 6 && isDigit(id) == true) {
        issuerId = id;
    }
    builder = new StringBuilder();
    for (int i = 0; i <= 9; i++) {
        int randomNum = (int) (Math.random() * (max - min + 1)) + min;
        builder.append(randomNum);
        accountNum = builder.toString();
    }
    setCheckDigit();
}

// C
private int checkSum() {
    int sum = 0;
    builder = new StringBuilder();
    builder.append(issuerId);
    builder.append(accountNum);
    for (int i = 0; i < builder.length(); i++) {
        // In each of the chars with an EVEN index
        if (i % 2 == 0) {
            int x = Integer.parseInt(Character.toString(builder.charAt(i))); //// get the int value from the char
            int y = x * 2; // multiply it by 2
            if (y >= 10) {
                int z = y % 10;
                z += 1; //// if doubling it has 2 digits, add those digits
                builder.setCharAt(i, Character.forDigit(z, 10)); // put above result back into the StringBuilder at
                                                                    // the same index
            }
        }
    }
    // Add the values of each digit in the StringBuilder
    for (int i = 0; i < builder.length(); i++) {
        sum += Integer.parseInt(Character.toString(builder.charAt(i)));
    }
    return sum;
}
//D
}
/*  public String toString() {
a public method called toString (NO PARAMETERS) that returns (in a 
return
statement) the issuerID, accountNum and checkDigit , BUT WITH A ' ' 
(space)
BETWEEN EVERY 4 CHARACTERS! (don't change any of the instance variables
here!)  
}
}
*/

所以我的主要问题是,指示说我必须返回这些变量(都超过 4 位),但每 4 个字符之间有一个分隔符 ' '。我需要一些指导来弄清楚如何实现“每 4 位数字”部分。也许使用 StringBuilder?请帮忙。

标签: java

解决方案


要返回每四个字符之间带有 ' ' 分隔符的字符串,请使用以下方法:

// Takes a String as input and outputs a formatted String:
public String toFormattedString(String inputString){
    StringBuilder returnStringBuilder = new StringBuilder();
    for(int i = 0; i < inputString.length(); i++) {
        returnStringBuilder.append(inputString.charAt(i));
        if(i%4==3) {
            returnStringBuilder.append(' ');
        }
    }
    return new String(returnStringBuilder);
}
// Takes a int as input and outputs a formatted String:
public String toFormattedString(int inputInt){
    return toFormattedString(Integer.toString(inputInt));
}

您可以在公共 String toString() 方法中使用这些方法。我只是不确定您希望如何在一个字符串返回值中返回三个字符串。您希望它们相互附加还是以不同的方式返回?


推荐阅读