0

I am really new to this. I am trying to use the Luhn Algorithm to validate a "person number" (a Swedish social security number). I think the code is almost finished but I don't know what to put in the class "birthDate.length()" and "pos" to make it work. birthDate.length needs to make sure that the string is 10 digits long and "pos" is needed for validation control.

package kund;
import java.util.Scanner;

public class Kund {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println("Welcome customer. Please login by using your "
                + "birthdate (yymmddxxxx)");
        Scanner input = new Scanner (System.in);
        String birthDate = input.next();

        int length = birthDate.length();
        int sum = 0;

        for (int i = 1; pos=length-1; i<=10; i++, pos--){
            char tmp = birthDate.charAt(pos);
            int num = Integer.parseInt(String.valueOf(tmp));

            int produkt;

            if (i % 2 != 0){
                produkt = num * 1;
            }else {
                produkt = num * 2;
            }
            if ( produkt > 9 )
                produkt -= 9;
            sum += produkt;

            boolean korrekt = (sum % 10) == 0;

            if (!korrekt){
                System.out.println("Invalid.");

            }else if(korrekt){
                System.out.println("Correct");
            }
        }
    }

}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Kangaroo
  • 9
  • 1
  • 5
  • So what's the question? – Mureinik Oct 13 '14 at 08:19
  • You do need to provide us with "how is it supposed to work" and "what exactly doesn't work". Now it's just some random piece of code. – Deltharis Oct 13 '14 at 09:25
  • Ok, sorry. Im gonna try. It's a class for a login for customers. They need to specify their personalnumber. Then the code checks if the number is real by multiplying the tenth digit with two, the ninth with one, the eight with two, the seventh with 1 etc. If the number adds up to >9 the code subtracts 9 from the number. All of the numbers should always add up to 50 and that is why "boolean korrekt = (sum % 10) == 0;" is put in there. – Kangaroo Oct 13 '14 at 11:49
  • The question is: I need the "birthDate.length();" to make sure the user doesnt input more or less than 10 digits and i need to fix the "pos" variable. – Kangaroo Oct 13 '14 at 11:55
  • And why does it show like 8 "Invalid" and 10 "Correct" instead of one? – Kangaroo Oct 13 '14 at 18:26

1 Answers1

1

your code is syntactical incorrect. Your for loop had 4 components in the loop head instead of 3 . I moved the pos=length-1; thing to a syntactical correct position. I do not know how the algorithm should work so, I cannot check whether the program is now semantically correct.

However, I assume you have a Swedish social security number and can easily check.

package kund;

import java.util.Scanner;

public class Kund {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        System.out.println("Welcome customer. Please login by using your "
                + "birthdate (yymmddxxxx)");
        Scanner input = new Scanner (System.in);
        String birthDate = input.next();

        int length = birthDate.length();
        int sum = 0;
        int pos = length-1;
        for (int i = 1; i<=10; i++, pos--){
            char tmp = birthDate.charAt(pos);
            int num = Integer.parseInt(String.valueOf(tmp));

            int produkt;

            if (i % 2 != 0){
                produkt = num * 1;
            }else {
                produkt = num * 2;
            }
            if ( produkt > 9 )
                produkt -= 9;
            sum += produkt;

            boolean korrekt = (sum % 10) == 0;

            if (!korrekt){
                System.out.println("Invalid.");
            }else if(korrekt){
                System.out.println("Correct");
            }
        }
    }
}
Matthias Steinbauer
  • 1,786
  • 11
  • 24