View Full Version : Reducing/Isolating digits using Mod
simplistic2099
01-30-2008, 02:58 AM
I am using the Luhn Algorithm and applying it to SIN numbers.
I need to isolate digits one through nine so what i've done to isolate the last digit in the SIN number is:
import.javax.swing.*;
public class LuhnAlg {
public static void main (String[]args) {
String input
int sin;
input = JOptionPane.showInputDialog ("Please type a nine digit social insurance number (SIN) with no spaces");
sin = Integer.parseInt (input)
my next step to isolate the ninth digit would be:
digit9 = sin%10
so basically 545454545 % 10 would become 5. Get it?
Now if i want to isolate the eith digit right after this step what would i have to do.
digit8 = (sin-digit9)%10
?? would this be right?
A1ien51
01-30-2008, 05:09 AM
Java != JavaScript
If you Google Java Luhn algorithm it should take you to the wikipedia. If you look there, there should be a link to a java version. Glad I could help you do your homework...
Eric
simplistic2099
01-31-2008, 12:27 AM
the wikipedia java code for the algorithmdidn't really work that well because it was too advanced. Im in early stages of java programming. BUT my final code worked like a charm!!!
import javax.swing.*;
import java.util.Date;
public class W_A_A1Q2
{
public static void main (String[]args)
{
String input;
int sin;
int digit9;
int digit8;
int digit7;
int digit6;
int digit5;
int digit4;
int digit3;
int digit2;
int digit1;
int checkdigit;
int total;
input = JOptionPane.showInputDialog ("Please type a nine digit social insurance number (SIN) with no spaces");
sin = Integer.parseInt (input);
digit9 = sin%10;
digit8 = (sin/10)%10;
digit8 = (((digit8 * 2) - 1)%9) + 1;
digit7 = (sin / 100)%10;
digit6 = (sin / 1000)%10;
digit6 = (((digit6 * 2) - 1)%9) + 1;
digit5 = (sin / 10000)%10;
digit4 = (sin / 100000)%10;
digit4 = (((digit4 * 2) - 1)%9) + 1;
digit3 = (sin / 1000000)%10;
digit2 = (sin / 10000000)%10;
digit2 = (((digit2 * 2) - 1)%9) + 1;
digit1 = (sin / 100000000);
total = digit9 + digit8 + digit7 + digit6 + digit5 + digit4 + digit3 + digit2 + digit1;
checkdigit = total%10;
System.out.println ("You have entered " + sin);
System.out.println ("The running total of the SIN number is:");
System.out.println (digit9 + " is added for a running total of " + digit9);
System.out.println (digit8 + " is added for a running total of " + (digit9 + digit8));
System.out.println (digit7 + " is added for a running total of " + (digit9 + digit8 + digit7));
System.out.println (digit6 + " is added for a running total of " + (digit9 + digit8 + digit7 + digit6));
System.out.println (digit5 + " is added for a running total of " + (digit9 + digit8 + digit7 + digit6 + digit5));
System.out.println (digit4 + " is added for a running total of " + (digit9 + digit8 + digit7 + digit6 + digit5 + digit4));
System.out.println (digit3 + " is added for a running total of " + (digit9 + digit8 + digit7 + digit6 + digit5 + digit4 + digit3));
System.out.println (digit2 + " is added for a running total of " + (digit9 + digit8 + digit7 + digit6 + digit5 + digit4 + digit3 + digit2));
System.out.println (digit1 + " is added for a running total of " + (digit9 + digit8 + digit7 + digit6 + digit5 + digit4 + digit3 + digit2 + digit1));
System.out.println ("The weighted total is: " + total);
System.out.println ("The check digit is: " + checkdigit);
System.out.println ("Programmed by COMP 1010 student ALEX WISHART");
System.out.println ("Date: " + new Date());
System.out.println ("***End of Processing***");
}
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.