Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## ASSIGNMENT HELP

i tried at least 50 times for this code to work and i just can't get it to work. can someone tell me how to do it?

Assignment - Repetition

In the nine-digit Social Insurance Number given to each person having a job or filing an income tax return in Canada, the ninth digit is a check digit that is used to test the validity of the other digits in the SIN. The ninth digit is determined by the following procedure.

a) Double the 2nd, 4th, 6th, and 8th digits.
 If the result is two digits long, add the digits together
b) Add the digits of the numbers found in step (a).
c) Add the 1st, 3rd, 5th, and 7th digits
d) Add the numbers found in steps (b) and (c).
e) Subtract the unit (ones digit) digit of the result of step (d) from 10 and note the units digit of the result. For the SIN to be valid, its ninth digit must have this value.

Write a program that repeatedly reads nine-digit numbers and determines whether or not each number is a valid SIN, The program should stop when it reads the value 999999999.  Reply With Quote

3. ## Re: ASSIGNMENT HELP

i tried at least 50 times
The instructions look reasonable.
Take them one at a time. What problem do you have doing the first step?  Reply With Quote

4. ## Re: ASSIGNMENT HELP Originally Posted by Norm Take them one at a time. What problem do you have doing the first step?
i don't know how to get each digit on it's own... i tried %10 to get the last digit but i don't understand how i would throw this all into a for loop!  Reply With Quote

5. ## Re: ASSIGNMENT HELP

Are you allowed to convert the number to a String? That would make getting the digits by their position easier.  Reply With Quote

6. ## Re: ASSIGNMENT HELP Originally Posted by Norm Are you allowed to convert the number to a String? That would make getting the digits by their position easier.
yeah you can do that! my teacher said you're allowed to do anything as long as it's in a loop but i dont know how to do it.. like i know the syntax but don't know how to apply it here.  Reply With Quote

7. ## Re: ASSIGNMENT HELP

What part of step one is giving you the problem?  Reply With Quote

8. ## Re: ASSIGNMENT HELP

i don't know how to convert it to from a int to a string then back ...  Reply With Quote

9. ## Re: ASSIGNMENT HELP

How does the program get the number's value initially? Is is read into a String?
If it starts as a String, then you don't need to convert it.
If it starts as an int, the String class has a method to convert it to a String.  Reply With Quote

10. ## Re: ASSIGNMENT HELP

what do you suggest would be the best way to do it  Reply With Quote

11. ## Re: ASSIGNMENT HELP  Reply With Quote

12. ## The Following User Says Thank You to Norm For This Useful Post:

ThePrince (April 18th, 2013)

13. ## Re: ASSIGNMENT HELP Originally Posted by Norm i'm a java noob... could you get my program started? like just until the loop cause i tried to put it as a string but i don't know how i would get the 2nd, 4th, 6th, 8th digits out of it! im STRUGGLING! thanks!  Reply With Quote

14. ## Re: ASSIGNMENT HELP

Start by putting in some import statements as needed.
Next define the class
next define the main() method.

Put off trading in the Strings. Define a String with a sample value and work on the steps listed in post#1
When that works, then look at using a loop to read in multiple Strings to be tested as in the above.  Reply With Quote

15. ## Re: ASSIGNMENT HELP

char c1 = SIN.charAt(1);
char c2 = SIN.charAt(3);
char c3 = SIN.charAt(5);
char c4 = SIN.charAt(7);
int num1 = c1*2;
if (num1>9){
Integer.toString(num1);
char num1_1 = ((num1.charAt(1))) +(num1.charAt(2));

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot invoke charAt(int) on the primitive type int
Cannot invoke charAt(int) on the primitive type int

i don't know what i did wrong, any suggestions? maybe where i should move the conversion statement  Reply With Quote

16. ## Re: ASSIGNMENT HELP

What is the definition of the variable: SIN? You have not posted enough of the code to see SIN's definition.

1)char values are not the same as int; '3' is not the same as 3. To get the 3 from '3' you need to subtract '0'
'3' - '0' = 3
2)The toString() method returns a String. The posted code does not assign the value returned by the toString() method to a variable.

[code=java]
[/code]
to get highlighting and preserve formatting.

Use the % and / operators to get the two digits from an int value between 10 and 99  Reply With Quote

17. ## Re: ASSIGNMENT HELP

```	// Prompt for SIN Card number as string, convert to integer to get check digit, output the check digit, convert back to string for proccess

String SIN = in.nextLine();
int SIN1 = Integer.parseInt(SIN);
int checkdigit = SIN1%10;
System.out.println("The checkdigit is: " + checkdigit);
Integer.toString(SIN1);

//Start the process

char c1 = SIN.charAt(1);
char c2 = SIN.charAt(3);
char c3 = SIN.charAt(5);
char c4 = SIN.charAt(7);
int num1 = c1*2;
if (num1>9){
Integer.toString(num1);
char num1_1 = ((num1.charAt(1))) +(num1.charAt(2));

}```

i'm completely lost... this is my first assignment using java, i have experience with python but this is on another level! help please! what should i do !!  Reply With Quote

18. ## Re: ASSIGNMENT HELP

char values are not the same as int; '3' is not the same as 3. To get the int value 3 from '3' you need to subtract '0'. For example: '3' - '0' = 3

To get the two digits from a number greater then 9 and less the 100 use the % and / operators:
13 % 10 = 3
13 / 10 = 1

`num1.charAt(1)`
You can not call methods for primitives. Methods go with objects  Reply With Quote

19. ## Re: ASSIGNMENT HELP

working on your program here. Taking some time but I am developing a solution for it.  Reply With Quote

20. ## The Following User Says Thank You to myjava For This Useful Post:

ThePrince (April 20th, 2013)

21. ## Re: ASSIGNMENT HELP

okay thanks so much! but why is it that when i put down 888888888 as the SIN number, and do this
```                char c1 = SIN.charAt(1);
char c2 = SIN.charAt(3);
char c3 = SIN.charAt(5);
char c4 = SIN.charAt(7);
int num1 = c1*2;

if (num1>9){
int num1_1 = (num1%10) + (num1/10);
System.out.println(num1%10);
System.out.println(num1/10);
System.out.println(num1_1);
}```

i get the results:
2
11
13

when i should be getting:
1
6
7

NOTE: i just put the system.out statements to see what each answer was, for the final code i would not display this.  Reply With Quote

22. ## Re: ASSIGNMENT HELP

What was the value of num1? You should print that also.

The value of the char '8' is not the int 8? Copy and execute this:
` System.out.println("'8'="+ (int)'8');  // '8'=56`
Also when printing out the values of variables, the output will be clearer if the values are labeled
`System.out.println("num1_1="+num1_1);`  Reply With Quote

23. ## Re: ASSIGNMENT HELP Originally Posted by Norm What was the value of num1? You should also print that also.
woah, i checked the value of num1 and i got 112!!
any thoughts on why? and yeah, ill do that for sure next time!  Reply With Quote

24. ## Re: ASSIGNMENT HELP

i checked the value of num1 and i got 112!!
any thoughts on why?
See the code in post#19 for an explanation. Look at an ASCII character table that gives the int values of all the characters.

See posts#14 and #16 for a technique to convert a char to an int  Reply With Quote

25. ## Re: ASSIGNMENT HELP Originally Posted by Norm What was the value of num1? You should print that also.

The value of the char '8' is not the int 8? Copy and execute this:
` System.out.println("'8'="+ (int)'8');  // '8'=56`
i copy and pasted it into the compiler and got '8' =56
so i would have to subtract '8' by 0 to get 8?  Reply With Quote

26. ## Re: ASSIGNMENT HELP

1. Take the user input as string

2. Select the digit at each place and save it into an array on integers. It can be done by
int numbers = new int;
for(int i = 0; i < 9; i++)
{
char a = stringAt(i);
numbers[i] = parse the char to integer.
}

3. int total = 0;
for loop(int i = 2; i < 9; i = i +2)
{ int temp = 0;
temp = numbers[i] * 2;
if(temp > 9)
total += temp/10;
total += temp%10;
else
total += temp;
}

4. for loop(int i = 1; i < 9; i = i+2)
{total += numbers[i];}

5. int test = total%10 - total/10;

6. if(test == numbers)
SSN is correct.
else
SSN is wrong

Hope this helps  Reply With Quote

27. ## Re: ASSIGNMENT HELP

so i would have to subtract '8' by 0 to get 8?
Did you try it to see what the results are? Write two lines of code to do the subtract and print out the results.  Reply With Quote

28. ## Re: ASSIGNMENT HELP

Try my solution from scratch. I have tried it with code. It won't take long. I can't pose code here because of the regulations.  Reply With Quote