# ASSIGNMENT HELP

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• April 20th, 2013, 03:39 PM
ThePrince
Re: ASSIGNMENT HELP
@myjava okay i'll try your code out! thanks man

--- Update ---

Quote:

Originally Posted by Norm
Did you try it to see what the results are? Write two lines of code to do the subtract and print out the results.

Code java:

```//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-0; num1 = c1*2; System.out.println(num1);```

i've tried subtracting it by 0 and '0'
i get 112 every time :-\$
• April 20th, 2013, 04:17 PM
Norm
Re: ASSIGNMENT HELP
Quote:

i've tried subtracting it by 0 and '0'
There is a big difference between the int 0 and the char '0'.
When you learned arithmetic, what was said about how a value change when you subtracted 0 from it?

Post the code that subtracts '0' from the char and prints out the results to show what problem you are talking about.

Code :

``` int num1 = c1-0; // sets the value of num1 num1 = c1*2; // immediately changes the value of num1```
The second line above completely replaces the value of num1 set by the first statement.
• April 20th, 2013, 04:33 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

Originally Posted by Norm
When you learned arithmetic, what was said about how a value change when you subtracted 0 from it?

the teacher was absent that day and we were expected to learn the lesson ourselves!:(
so what do you think i should do? subtract it by 0 after the if (num1>9) or before?
• April 20th, 2013, 04:41 PM
Norm
Re: ASSIGNMENT HELP
As I said several times before, one way to convert a digit char to an int value is to subtract '0' from it. For example: '3' - '0' = 3
The steps would be:
get a char
convert the char to an int value by subtracting '0' from it.
Then double the int value
then compare the doubled int to 9

Quote:

subtract it by 0
0 is not the same value as '0'. The 's are important
• April 21st, 2013, 12:30 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

Originally Posted by Norm
As I said several times before, one way to convert a digit char to an int value is to subtract '0' from it. For example: '3' - '0' = 3

okay it works now, but how would i throw this all into a loop. i don't get how i would be able to set up a for loop and have it check if each number doubled is greater than 9 and then individually add them together.. help please?

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-'0'; //This will get a integer value for a character num1 = num1*2; int num2 = c2-'0'; num2 = num2*2; int num3 = c3-'0'; num3 = num3*2; int num4 = c4-'0'; num4 = num4*2;   if (num1>9){ System.out.println(num1); int num1_1 = (num1%10) + (num1/10); }```
• April 21st, 2013, 01:06 PM
Norm
Re: ASSIGNMENT HELP
Does the code now completely check a SIN to be correct?
Once the code is able to check a SIN for correctness, then a loop can be made to get another SIN and check it.
Something like this:
begin loop
get SIN
check that SIN is correct
end loop

The loop is not used for checking the correctness of a SIN.
• April 21st, 2013, 01:53 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

Originally Posted by Norm
Does the code now completely check a SIN to be correct?

yeup! i finally got it to work, but I'm having one last problem..
the program is supposed to keep running until 999999999 is entered

so if i create a while loop for example:

while (SIN!=999999999) {
<entire code here>
}

it would keep running infinitely, if i choose to prompt again at the end of the code i would have to change the while loop variable and thus making it not work.

loop
get sin
check sin
end loop
but i cant seem to get the variables set
• April 21st, 2013, 02:06 PM
Norm
Re: ASSIGNMENT HELP
My example was very simple and didn't show any tests to end the loop. a test would need to be added after the SIN was read to check for the request to end the loop and the loop exited there. A break statement is one way to exit a loop.
• April 21st, 2013, 04:04 PM
ThePrince
Re: ASSIGNMENT HELP
Quote:

Originally Posted by Norm
My example was very simple and didn't show any tests to end the loop. a test would need to be added after the SIN was read to check for the request to end the loop and the loop exited there. A break statement is one way to exit a loop.

yeup, i finally got the damn thing to work. thanks for your help norm. much appreciated man.
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12