Click to See Complete Forum and Search --> : Help: Number Verification Using String, Please Check=(


Adeno
October 5th, 2006, 07:10 PM
Hi guys=( I'm in a little trouble=( I got sick for a whole week and we have a due mini-project soon. I really tried hard trying to figure this out for myself (and of course by asking classmates too) but everyone I approached seem to be just as lost as I am. It would be great if someone could help me=) This is what we're supposed to do:

=====================================================
You will need to store each digit of the social insurance number in a field of its own. To validate the entry you will:

1. Multiply the 2nd, 4th, 6th and 8th digit by 2.
2. If the product of any of the four multiplications result in a value greater than 9, add the two resulting digits together to yield a single-digit response. For example 6 * 2 = 12, so you would add the 1 and the 2 to get a result of 3.
3. Add these four calculated values together, along with the 1st, 3rd, 5th, and 7th digits of the original number.
4. Subtract this sum from the next highest multiple of 10.
5. The difference should be equal to the 9th digit, which is considered the check digit.

Example of validating S.I.N. 765932546

1st digit 7
2nd digit (6*2 =12 1+2=) 3
3rd digit 5
4th digit (9*2 = 18 1+8 =) 9
5th digit 3
6th digit (2*2 = 4) 4
7th digit 5
8th digit (4*2 = 8) 8

Total 44 next multiple of 10 is 50
50-44 = 6 which is the 9th digit

Therefore the S.I.N. 765932546 is Valid



**********************************
********* SIN Validation *********
**********************************

Welcome - Please enter the first number: 120406780
Second digit value multiplied by 2 4
Fourth digit value multiplied by 2 8
Sixth digit value multiplied by 2 12
Eighth digit value multiplied by 2 16
Value derived from 6th digit 3
Value derived from 8th digit 7
The total is 30
Calculated digit is 10
Check digit must be zero because calculated value is 10
The SIN 120406780 is Valid
=====================================================



So that's what we're supposed to do, and this is what I have come up with so far:


=====================================================
import java.util.Scanner;
public static class lab1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int sin = 0;
int num0, num1, num2, num3, num4, num5, num6, num7, num8;
int count = 0;
int second, fourth, sixth, eighth;

System.out.println("Please enter a sin number: ");
sin = input.nextInt();

count = int.length(sin);

while (count > 8 || count < 8)
{
System.out.println("Valid Input");
System.out.println("Please enter a sin number: ");
sin = input.nextInt();
}

num0 = input.next().charAt(0);
num1 = input.next().charAt(1);
num2 = input.next().charAt(2);
num3 = input.next().charAt(3);
num4 = input.next().charAt(4);
num5 = input.next().charAt(5);
num6 = input.next().charAt(6);
num7 = input.next().charAt(7);
num8 = input.next().charAt(8);

second = num2 * 2;
fourth = num4 * 2;
sixth = num6 * 2;
eighth = num8 * 2;
}
}

=====================================================


Can someone please try this? I always get an error and I'm really really stumped as to what I'm missing. I have an idea of how to do the program, just that since I'm still a beginner, I do get lost from time to time (plus the fact that I got sick and missed out on class)=(

Your help would be greatly appreciated, I really hope somebody could show me how to fix this, and tell me in detail what I'm missing=( Thanks in advance guys!

dlorde
October 6th, 2006, 04:44 AM
If you step through the problem by hand with pencil and paper, away from the computer, you will see all the steps that are required. Make a note of them. If you step through your code the same way, you will see what is missing. The code simply repeats the steps you would use to do it by hand.

I suggest you look particularly carefully at what you want for input and what you want to do with it. Do you want an int or a String - which is easier to manipulate digit by digit?

If you have a particular problem or question, please ask it.

p.s. There is no int.length(..) method.
p.p.s. You go to some trouble to get an int input (sin), but then you ignore it - why?
p.p.p.s. what are all the numX = input.next().charAt(0); lines for?

The truth is, when all is said and done, one does not teach a subject, one teaches a student how to learn it. Teaching may look like administering a dose, but even a dose must be worked on by the body if it is to cure. Each individual must cure his or her own ignorance...
J. Barzun