表示一个包含4个以上字符的字符串作为4个字符

时间:2018-05-30 03:36:52

标签: java

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位数),但带有分隔符&#39; &#39;每4个字符之间。我需要一些指导来确定如何实现&#34;每4个数字&#34;部分。也许使用StringBuilder?请帮忙。

1 个答案:

答案 0 :(得分:0)

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

// 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()方法中使用这些方法。我只是不确定您希望如何在一个String返回值中返回三个String。您是希望它们彼此附加还是以其他方式返回?