# ASSIGNMENT HELP

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• April 17th, 2013, 11:59 AM
ThePrince
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.
• April 17th, 2013, 12:05 PM
Norm
Re: ASSIGNMENT HELP
Quote:

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?
• April 17th, 2013, 12:15 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

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!
• April 17th, 2013, 12:24 PM
Norm
Re: ASSIGNMENT HELP
Are you allowed to convert the number to a String? That would make getting the digits by their position easier.
• April 17th, 2013, 02:15 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

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.
• April 17th, 2013, 02:20 PM
Norm
Re: ASSIGNMENT HELP
What part of step one is giving you the problem?
• April 17th, 2013, 03:15 PM
ThePrince
Re: ASSIGNMENT HELP
i don't know how to convert it to from a int to a string then back ...
• April 17th, 2013, 03:25 PM
Norm
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.
• April 18th, 2013, 07:47 PM
ThePrince
Re: ASSIGNMENT HELP
what do you suggest would be the best way to do it
• April 18th, 2013, 08:09 PM
Norm
Re: ASSIGNMENT HELP
• April 18th, 2013, 08:29 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

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!
• April 18th, 2013, 08:35 PM
Norm
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.
• April 18th, 2013, 09:24 PM
ThePrince
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
• April 19th, 2013, 05:56 AM
Norm
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
• April 20th, 2013, 02:42 PM
ThePrince
Re: ASSIGNMENT HELP
Code java:

``` // Prompt for SIN Card number as string, convert to integer to get check digit, output the check digit, convert back to string for proccess   System.out.println("Please enter your SIN card number: "); 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 !!
• April 20th, 2013, 02:51 PM
Norm
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

Code :

`num1.charAt(1)`
You can not call methods for primitives. Methods go with objects
• April 20th, 2013, 02:55 PM
myjava
Re: ASSIGNMENT HELP
working on your program here. Taking some time but I am developing a solution for it.
• April 20th, 2013, 03:02 PM
ThePrince
Re: ASSIGNMENT HELP
okay thanks so much! but why is it that when i put down 888888888 as the SIN number, and do this
Code java:

``` 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.
• April 20th, 2013, 03:07 PM
Norm
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:
Code :

` 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
Code :

`System.out.println("num1_1="+num1_1);`
• April 20th, 2013, 03:10 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

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!
• April 20th, 2013, 03:12 PM
Norm
Re: ASSIGNMENT HELP
Quote:

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
• April 20th, 2013, 03:18 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

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:
Code :

` 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?
• April 20th, 2013, 03:23 PM
myjava
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[9];
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[8])
SSN is correct.
else
SSN is wrong

Hope this helps
• April 20th, 2013, 03:30 PM
Norm
Re: ASSIGNMENT HELP
Quote:

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.
• April 20th, 2013, 03:32 PM
myjava
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last