2

I have a task with checking ID number and I must check if this ID has 11 characters, if those characters are digits and I must check control number. Number is correct when this equation is correct:

ID = abcdefghijk

(1*a+3*b+7*c+9*d+1*e+3*f+7*g+9*h+1*i+3*j+1*k) % 10 = 0

Sample correct ID is: 49040501580

And here is my program. I don't know how to check if ID is digit and why it isn't correct. Anyone help? XD Thank you in advance :3

import java.util.*;

public class wat {

    public static void main(String[] args) {
        char[] weights = {1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1};
        System.out.print("Enter next digits your ID number: ");
        Scanner keyboard = new Scanner(System.in);
        String number = keyboard.nextLine();
        char[] ofm = number.toCharArray();
        Character[] id = new Character[ofm.length];
        for (int i = 0; i < ofm.length; i++) {
            id[i] = ofm[i];
            System.out.print(id[i] + " ");
            int length = id.length;
            if (length == 11) {
                System.out.println("This ID number has 11 digits");
                System.out.println("Checking of the control number");
                int amount = 0;
                amount = id[i] * weights[i];
                System.out.println(amount);
                int result = 0;
                result = amount % 10;
                if (result == 0) {
                    System.out.println("ID number is correct");
                } else {
                    System.out.println("ID number is not correct");
                    break;
                }
            } else {
                System.out.print("This ID number hasn't 11 digits.");
                break;
            }
        }
    }

}

Sample output

Unheilig
  • 16,196
  • 193
  • 68
  • 98
zuczek zuk
  • 21
  • 1
  • 5
  • 2
    Post code here, not as a link to an off-site resource. Paste, highlight, and press ctrl-k. – Elliott Frisch Nov 09 '15 at 00:28
  • To check for digits: http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java the rest should be trivial. – PeteB Nov 09 '15 at 00:34
  • @PeteB I am a beginner this isn't so trivial for me ;) – zuczek zuk Nov 09 '15 at 00:40
  • Fair enough :) Can you provide sample input/output for your program? The code looks mainly ok although there are some superficial improvements like checking the length before the loop etc. – PeteB Nov 09 '15 at 00:56
  • Ah I see one problem... you need to add onto 'amount' every time around the loop and then check the *final* value, currently you're testing each character one at a time so the equation is never getting the whole value. – PeteB Nov 09 '15 at 00:59
  • Can you tell me why System.out.print(id[i] + " "); show only first number? :) And what do you mean by "add onto 'amount' every time around the loop and then check the final value," I don't really know what I should do XD – zuczek zuk Nov 09 '15 at 01:15

1 Answers1

0

With a minimal number of changes to your original code, I believe this is what you need.

import java.util.*;
     public class wat {
            public static void main(String[] args) {
                    char[] weights = {1,3,7,9,1,3,7,9,1,3,1};
                    System.out.print("Enter next digits your ID number: ");
                    Scanner keyboard = new Scanner(System.in);
                    String number = keyboard.nextLine();
                    char[] ofm=number.toCharArray();
                    Character[] id=new Character[ofm.length];
                    if (ofm.length == 11) {
                        System.out.println("This ID number has 11 characters");
                        int amount = 0;
                        for (int i = 0; i < ofm.length; i++) {
                                id[i]=ofm[i];
                                System.out.print(id[i]+" ");
                                int length = id.length;
                                if (isDigit(id[i])) {
                                        amount = id[i]*weights[i];
                                } else {
                                    System.out.println("character is not a digit");
                                    break;
                                }
                        }
                    } else {
                        System.out.print("This ID number hasn't 11 digits.");
                        return;
                    }
                    System.out.println("Checking of the control number");
                    System.out.println(amount);
                    int result =0;
                    result = amount % 10;
                    if (result == 0) {
                        System.out.println("ID number is correct");
                    } else {
                        System.out.println("ID number is not correct");
                    }
            }
    }

As you can see, we're now verifying the string length before the loop starts.

Then we're checking each character is a digit one by one and quitting with a new error message if one of them is not. You'll need to provide the isDigit function yourself (plenty to choose from on StackOverflow!).

We're accumulating the digit values multiplied by the weight into the amount variable, with a new value being added each time the loop iterates.

Finally we verify the result is correct after the loop has finished and all 11 characters have been processed.

NOTE: I don't normally work in Java so I'm not entirely sure if you can get the digit value out of a Character type object (to multiply it with the weight) like this. You might find that you are getting ASCII code values, or something else entirely. If this happens, you'll need to convert 'weights' into an array of integers, and extract the actual numeric value from the Character before multiplying them together.

PeteB
  • 372
  • 1
  • 10
  • In regards to isDigit, I see from Java docs (http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html) that Character has isLetter and isLetterOrDigit but doesn't have an isDigit member (weird!). You can use the other two to say if (ch.isLetterOrDigit() && !ch.isLetter()) to get the effect of an isDigit check. – PeteB Nov 09 '15 at 01:15
  • Thank you. I'm still working on it, but now certainly it'll be easier for me :) – zuczek zuk Nov 09 '15 at 01:33
  • That's great :) This might be useful if you need to get the digit value out of the Character: http://www.tutorialspoint.com/java/lang/character_digit.htm – PeteB Nov 09 '15 at 01:43