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. ## Re: ASSIGNMENT HELP

@myjava okay i'll try your code out! thanks man

--- Update ---

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.
```//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

2. ## Re: ASSIGNMENT HELP

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.

```     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.

3. ## Re: ASSIGNMENT HELP

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?

4. ## 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

subtract it by 0
0 is not the same value as '0'. The 's are important

5. ## Re: ASSIGNMENT HELP

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?

```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);
}```

6. ## 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.

7. ## Re: ASSIGNMENT HELP

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

8. ## 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.

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

ThePrince (April 21st, 2013)

10. ## Re: ASSIGNMENT HELP

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.

Page 2 of 2 First 12