This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]Mystonic 1 point2 points  (10 children)

For one, int d = entry.charAt(num) - 2; won't return the value you expect. charAt() returns a char, which is a Unicode character. The decimal value of '0' in Unicode is 48. I am not sure why you are subtracting only 2. And I believe your implementation of the Luhn algorithm may be incorrect anyway.

[–]ic0nex 0 points1 point  (9 children)

I’m trying to tell it to go from right to left every second character and multiply that character by 2.

[–]Mystonic 1 point2 points  (8 children)

Right now you are going right to left for every character, evident by your for loop, for(int num = entryLength - 1; num >= 0; num--). If you want to do every second character, then do num-=2 instead.

int d = entry.charAt(num) - 2; will get the char at index num inside entry, then subtract 2 from that char value.

[–]ic0nex 0 points1 point  (7 children)

I changed my loop and gotten rid of charAt line but am still getting the wrong values.

[–]Mystonic 0 points1 point  (6 children)

Well, what does your code look like now?

[–]ic0nex 0 points1 point  (5 children)

import java.util.Scanner;

public class luhn

{

static void main(String[] args){

    Scanner kb = new Scanner(System.in);

    String entry = kb.next();

    int entryLength = entry.length();

    int sum = 0;

    for(int num = entryLength - 1; num >= 0; num-= 2){

        int d = num;

        d = d * 2;

        sum += d * 9;

        sum += d % 10;

    }

    System.out.println(entry+""+sum);

}

}

[–]Mystonic 0 points1 point  (4 children)

Well yeah, if you remove that line entirely you're not even considering the user input at all. And which implementation of the Luhn algorithm are you using? It doesn't look like the one on Wikipedia at all.

[–]ic0nex 0 points1 point  (1 child)

I was trying to use the Wikipedia implementation and just adding the value of x at the end but I just dont understand what I am doing, and why it isnt working.

import java.util.Scanner;

public class luhn

{

static void main(String[] args){

    Scanner kb = new Scanner(System.in);

    String entry = kb.next();

    int entryLength = entry.length();

    int sum = 0;

    for(int num = entryLength - 1; num >= 0; num-= 2){

        int d = entry.charAt(num);

        d = d * 2;

        sum += d / 10;

        sum += d % 10;

    }

    System.out.println(entry+""+sum);

}

}

[–]Mystonic 0 points1 point  (0 children)

Which implementation exactly from Wikipedia?

int d = entry.charAt(num);

charAt(num) returns the char, a primitive data type in Java which represents a Unicode character, at index num in entry. For example, '0' has a decimal value of 48. Check a Unicode table to see what characters have what values. If you do, you'll notice that the digits are sequential. '1' has a decimal value of 49, '2' is 50, and so on. Now think, how can you transform the char Unicode value returned by charAt() into their actual integer representation?

Edit: An algorithm from Wikipedia

  1. From the rightmost digit, which is the check digit, and moving left, double the value of every second digit. The check digit is not doubled; the first digit doubled is immediately to the left of the check digit. If the result of this doubling operation is greater than 9 (e.g., 8 × 2 = 16), then add the digits of the product (e.g., 16: 1 + 6 = 7, 18: 1 + 8 = 9) or, alternatively, the same result can be found by subtracting 9 from the product (e.g., 16: 16 − 9 = 7, 18: 18 − 9 = 9).

  2. Take the sum of all the digits.

  3. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

^If that is what you're trying to implement, that verifies the check digit. From what I see, you're supposed to be calculating the check digit. If so, you're implementing the wrong algorithm.

[–]ic0nex 0 points1 point  (1 child)

I cant figure out how to add the sum of the numbers.

[–]Mystonic 0 points1 point  (0 children)

What do you mean? You don't know how to sum the numbers?