-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!)  
}
}
*/

So my main issue here is, the directions say that I have to return these variables (all more than 4 digits) but with a delimiter ' ' between every 4 characters. I need some guidance into figuring out how to implement the "every 4 digits" part. Maybe using a StringBuilder? Please help.

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
  • 1
    Take a look at https://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java – Thiyagu May 30 '18 at 04:11
  • 3
    Is this your friend https://stackoverflow.com/questions/50594447/how-do-we-return-variables-with-a-space-between-every-4-characters-java Same question/code posted earlier today – Scary Wombat May 30 '18 at 04:16
  • Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Venki WAR May 30 '18 at 04:24

1 Answers1

0

To return a String with ' ' delimiters in between every four characters, use these methods:

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

You can use these methods in your public String toString() method. I'm just not sure how you want the three Strings to be returned in one String return value. Do you want them appended to each other or returned in a different way?

jcroskery
  • 135
  • 2
  • 10