Results 1 to 3 of 3
Thread: Masking a credit card value
- 12-02-2009, 08:39 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
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();
}
- 12-02-2009, 08:50 PM #2
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'sLiberty 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.
- 12-02-2009, 09:05 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 5
- Rep Power
- 0
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
-
Masking password for logging
By firewalll in forum New To JavaReplies: 1Last Post: 09-29-2009, 08:04 AM -
Card
By hedonist in forum New To JavaReplies: 3Last Post: 08-13-2009, 02:20 PM -
Java Project for extra Credit
By Ankiel24 in forum New To JavaReplies: 5Last Post: 04-24-2009, 09:56 AM -
Credit Card Validator
By bluegti02 in forum New To JavaReplies: 2Last Post: 06-17-2008, 06:09 AM -
[SOLVED] File I/O Extra Credit Assignment FTW!
By Bascotie in forum New To JavaReplies: 9Last Post: 06-10-2008, 08:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks