# remainder issue

• September 14th, 2013, 07:37 AM
nepperso
remainder issue
I'm currently taking a class in learning java.

Anyways, I am doing a code that was provided and has an error.
We are suppose to review and figure out why it's giving the error and how to fix it.

You coded the following in class Test.java:
Code :

```int a = 32; int b = 10; double c = a / b; System.out.println( “The value of c is “ + c );```
The code compiles properly and runs, but the result is not what you
expected. The output is
The value of c is 3.0
You expected the value of c to be 3.2. Explain what the problem is and
how to fix it.

So the problem is the left over value. In the chapter I read it spoke about using % to bring out the remainder. But all the options I could think of wouldn't work.
This is what I wrote with some modification on the original script
Code :

``` int a = 32, b = 10; double c = (double)a/b; c%=c; //tried to float c but that gives the same answer. The issue I believe is a rounding // I know % would be right trick to pull the extra digits. System.out.println("The value of c is " + c);   System.exit(0);```

I would appreciate any assistance on this

I also tried playing around with
Code :

``` int a, b; double c;   b = 10; a = 32; c = a%b;```
• September 14th, 2013, 07:45 AM
GregBrannon
Re: remainder issue
Integers (ints) are counting numbers that can't by themselves provide results that include fractional, decimal, or floating point parts. What other data types have you learned that might provide the decimal portion in the result from a division?

Edit: Let me say that differently/more clearly: Either the numerator, denominator, or both must be a non-integer data type to provide the results you seek.
• September 14th, 2013, 07:54 AM
nepperso
Re: remainder issue
when dealing with decimals that data types that I can think that would work would be double, float, and maybe long but this division isn't really a long string.
I tried re-writing as float and also changing all to double and that didn't work, so now I'm really stumped. And I know one of the data types MUST be a non-integer for it to work. I am just drawing a blank on how to tell it that. I than tried this to which SHOULD work cause i am telling it to turn the numerator into a non-integer:
Code :

``` int a, b; double c;   b = 10; a = 32; c = ((double)b)/a;   //tried to float c but that gives the same answer. The issue I believe is a rounding // I know % would be right trick to pull the extra digits. System.out.println("The value of c is " + c);```
• September 14th, 2013, 08:05 AM
GregBrannon
Re: remainder issue
You're on the right track, but I'm not sure why you changed the division in the provided code from a / b to b / a. Did you try:

c = (a / (double)b);
• September 14th, 2013, 08:18 AM
nepperso
Re: remainder issue
I did not try that particular way, I tried c = a / ((double)/b);
Let me try that...
Nope! wtf??? why is it not working??
• September 14th, 2013, 08:22 AM
GregBrannon
Re: remainder issue
Post your updated code, and we'll figure it out.
• September 14th, 2013, 10:09 AM
nepperso
Re: remainder issue
Code :

``` public static void main(String [] args) { int a = 32; int b = 10; double c = (a/(double)b);   //The issue is the order of operations somehow, it is still thinking a & b are int therefor // even though C is a double, it is still rounding. System.out.println("The value of c is " + c);   System.exit(0); } {```

--- Update ---

Also tried:
Code :

``` int a, b; double c; a = 32; b = 10; c = (a/(double)b);```
• September 14th, 2013, 10:15 AM
GregBrannon
Re: remainder issue
The first version you posted results in 3.2. Isn't that the result you're trying to get?

Are you working from the command line? Or using an IDE? You may be running the old version. Delete the .class files, recompile, and run the result. You should get 3.2.
• September 14th, 2013, 10:28 AM
nepperso
Re: remainder issue
I am using IDE

--- Update ---

yay! it works! good to know that issue can happen. was driving me NUTS!!!
I was like this should work wth!
• September 14th, 2013, 11:58 AM
GregBrannon
Re: remainder issue
Glad you figured it out. Sometimes (don't know why) the IDE doesn't recognize that the source file(s) have changed so uses the old .class files without recompiling. It doesn't happen often, maybe it's time to restart the IDE when it does, and it's frustrating.