aspose file tools*
The moose likes Java in General and the fly likes Modulus 10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Modulus 10" Watch "Modulus 10" New topic
Author

Modulus 10

Ray Marsh
Ranch Hand

Joined: Jan 12, 2000
Posts: 458
Not sure where this question should go.
Has anyone heard of a mod10 check in Java? Perhaps a class already exists that handles the logic.
Modulus 10 for those who are unfamiliar is a format check for credit card numbers. It does not authorize dollar amounts or even check if the account is active. All it does is perform a complicated algorithm on the credit card number to see if it is valid. Example '1111 1111 1111 1111' would fail. There are certain patterns that must exist in a CC number for it to pass mod10.
Thanks in advance.

Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
Glen Tanner
Ranch Hand

Joined: Apr 16, 1999
Posts: 147

There's a post on the Sun site with a code example, but I'm not sure if that is what you're looking for.
http://forum.java.sun.com/forum?14@@.eef01e5
Ray Marsh
Ranch Hand

Joined: Jan 12, 2000
Posts: 458
Thanks Glen. I'll check it out.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
I've just tried the code from that site. It's a bit of a mess, but it does work.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Glen Tanner
Ranch Hand

Joined: Apr 16, 1999
Posts: 147

Sorry, I should have at least tried the code!
Ray Marsh
Ranch Hand

Joined: Jan 12, 2000
Posts: 458
Originally posted by Frank Carver:
I've just tried the code from that site. It's a bit of a mess, but it does work.

Thanks Frank.
How did you test the code? I'm having some trouble with it.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Well I reformatted it, wrapped it up in a class and tried it with the numbers on my credit cards, and a few other known wrong ones.

For obvious reasons I'm reluctant to give you my test data!
Ray Marsh
Ranch Hand

Joined: Jan 12, 2000
Posts: 458
Thanks Frank.
I don't need ALL your test data. Just your CC number, expiration date and credit limit. But I promise not to tell anyone!
Manju Swamy
Greenhorn

Joined: Mar 03, 2000
Posts: 14
Here is an improvised version. This does not have a card number digits limitation. Also when use isDigit() function it reruns true for various other language (like Indian, Arabic, etc.) numbers. Then the whole logic would fail.

[This message has been edited by Manju Swamy (edited March 03, 2000).]
[This message has been edited by Manju Swamy (edited March 03, 2000).]


Mind is like a parachute. It works only when it is open.
Ray Marsh
Ranch Hand

Joined: Jan 12, 2000
Posts: 458
Thank you, Manju.
I'll give that one a test drive as well.
I have a modulus 10 routine in RPG that has been in production for a few years. The specs for the logic is on IBM's AS400 book shelf web-site in their DDS reference manual.
Once I have one or two working Java programs, I plan to run some parallel testing. It will be interesting to see what happens.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Why are you doing all that math? Putting the results of the calculations in a static array would make it much faster/easier to read. Pseudocode follows.
public class CCThingy
{
public static int[] results = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
public boolean checkIt(int ccarray[])
{
// Sanity checking removed, you can figure
// that out for yourselves.
boolean flip = false;
int sum = 0;
for(int i = ccarray.length; i >= 0; i--)
{
if(flip)
{
sum += results[ccarray[i]];
} else {
sum += ccarray.length[i];
}
}
return (sum % 10 == 0);
}

This way you don't have to calculate values that aren't going to ever change. And you don't have to multiply something by 1.


------------------
--I'm Hugh Jass.
Mark Barell
Greenhorn

Joined: Sep 06, 2001
Posts: 2
But is there a way to check the expire date too ?
 
 
subject: Modulus 10