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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: remainder issue

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default 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:
    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
            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
            int a, b;
            double c;
     
            b = 10;
            a = 32;
            c = a%b;


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    nepperso (September 14th, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default 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:
            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);

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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);

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    nepperso (September 14th, 2013)

  7. #5
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default 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??

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: remainder issue

    Post your updated code, and we'll figure it out.

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

    nepperso (September 14th, 2013)

  10. #7
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: remainder issue

        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:
            int a, b;
            double c;
            a = 32;
            b = 10;
            c = (a/(double)b);

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

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

    nepperso (September 14th, 2013)

  13. #9
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default 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!

  14. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

Similar Threads

  1. [SOLVED] Math Remainder Issue
    By bzhagar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 12th, 2012, 07:41 PM
  2. Java Issue / Cache issue
    By VisualPK in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2012, 08:43 PM
  3. not sure what the issue is here
    By Tfence in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2011, 02:18 AM
  4. Remainder Assignment Operator Help
    By jsam0123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2011, 06:04 PM
  5. Chinese Remainder therom
    By Joy123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 16th, 2011, 07:11 AM

Tags for this Thread