Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. Reducing/Isolating digits using Mod

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&#37;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?

• 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

• 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&#37;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***");

}

}

•

Posting Permissions

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