Results 1 to 3 of 3
  1. #1
    Samurai Coder is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default Masking a credit card value

    The following code produces a mask that looks like 4xxx-xxxx-xxxx-1221

    We want a mask that is simply the last 4 digits.

    How would I modify this code to get that result?

    public static String maskNumber(String accno) {
    StringBuffer sb = new StringBuffer();
    if (StringUtils.isNotBlank(accno)) {
    int length = accno.length();
    int mask = length;
    if (length > 7) {
    mask = length - 4;

    for (int i = 0; i < mask; i++) {
    if (i % 4 == 0) {
    sb.append("-");
    }
    sb.append("x");
    }
    sb.reverse();
    sb.append(accno.substring(mask));
    sb.replace(0, 1, String.valueOf(accno.charAt(0)));

    } else {
    // if account.length too short, do not show length and mark
    // as xxxxxN
    mask = length - 1;

    sb.append("xxxxx");
    sb.append(accno.substring(mask));
    if (length > 4) {
    sb.replace(0, 1, String.valueOf(accno.charAt(0)));
    }
    }
    }
    return sb.toString();
    }

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    start with the unmasked number. Then add 4, -, 4, -, 4, -, xxxx

    I mean essentially copy your unmasked string into a new string builder starting from index 0 and once you hit index[length-5] just put x's
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    Samurai Coder is offline Member
    Join Date
    Dec 2009
    Posts
    5
    Rep Power
    0

    Default reverse no more

    Thanks. I think thats essentially what I get if I reverse engineer what the previous coder has done. Like so...

    public static String maskNumber(String accno) {
    StringBuffer sb = new StringBuffer();
    if (StringUtils.isNotBlank(accno)) {
    int length = accno.length();
    int mask = length;
    if (length > 7) {
    mask = length - 4;

    sb.append(accno.substring(mask));


    } else {
    // if account.length too short, do not show length and mark
    // as xxxxxN
    mask = length - 1;

    sb.append("xxxxx");
    sb.append(accno.substring(mask));
    if (length > 4) {
    sb.replace(0, 1, String.valueOf(accno.charAt(0)));
    }
    }
    }
    return sb.toString();
    }

Similar Threads

  1. Masking password for logging
    By firewalll in forum New To Java
    Replies: 1
    Last Post: 09-29-2009, 09:04 AM
  2. Card
    By hedonist in forum New To Java
    Replies: 3
    Last Post: 08-13-2009, 03:20 PM
  3. Java Project for extra Credit
    By Ankiel24 in forum New To Java
    Replies: 5
    Last Post: 04-24-2009, 10:56 AM
  4. Credit Card Validator
    By bluegti02 in forum New To Java
    Replies: 2
    Last Post: 06-17-2008, 07:09 AM
  5. [SOLVED] File I/O Extra Credit Assignment FTW!
    By Bascotie in forum New To Java
    Replies: 9
    Last Post: 06-10-2008, 09:20 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •