Results 1 to 5 of 5
  1. #1
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default Simple Addition Program Outputting Wrong Value

    [CODE]import ccj.*;

    public class Coins3
    {
    public static void main(String[] args)
    {
    /* User Input */
    System.out.println("How many pennies do you have?");
    int pennies = Console.in.readInt();
    System.out.println("How many nickels do you have?");
    int nickels = Console.in.readInt();
    System.out.println("How many dimes do you have?");
    int dimes = Console.in.readInt();
    System.out.println("How many quarters do you have?");
    int quarters = Console.in.readInt();
    /* Calculate totals */
    double total = (pennies * 0.01) + (nickels + 0.05) + (dimes * 0.10) + (quarters * 0.25);
    System.out.println("Total value = " + total);
    }
    }[CODE]
    When I run the program in command prompt with command "java Coins3" and type "1" for each penny, nickel, dime, and quarter, I receive an output of 1.4100000001 instead of 0.41.

    By the way, CCJ is a class package designed for the book I'm reading "Computing Concepts with JAVA Essentials". The Console.in.readInt() function basically asks user input.

    The obvious answer is 0.41, but why is it adding 1 to that total?

    Thanks!
    Last edited by carlodelmundo; 08-04-2008 at 04:45 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Are you sure that you can compile this code? Console class doesn't have readInt() method as far as I know.

  3. #3
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    I feel the following is wrong somewhere
    Java Code:
    double total = (pennies * 0.01) + (nickels + 0.05) + (dimes * 0.10) + (quarters * 0.25);

    "(nickels + 0.05)" should have been "(nickels * 0.05)" isnt it?
    To finish sooner, take your own time....
    Nivedithaaaa

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    output of 1.4100000001 instead of 0.41.
    That's because computers store numbers in binary not decimal. There are approximations made when converting a real decimal number to binary. If you use int the answer will always be correct.
    To use int, store your values as cents vs dollars. For example a nickle would be 5 cents vs .05 dollars. You'll have to format the output to get it to print as dollars.

  5. #5
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by Niveditha View Post
    Hi,
    I feel the following is wrong somewhere
    Java Code:
    double total = (pennies * 0.01) + (nickels + 0.05) + (dimes * 0.10) + (quarters * 0.25);

    "(nickels + 0.05)" should have been "(nickels * 0.05)" isnt it?
    Ahh. Thank you! That solved my problem!!

    To Norm: Interesting. I'll convert it to cents instead of multiplying it by a decimal value (.01) to avoid this estimation.

    Thanks all

Similar Threads

  1. what is wrong with this program ?
    By Poor Bee in forum New To Java
    Replies: 1
    Last Post: 05-07-2008, 07:23 PM
  2. Replies: 0
    Last Post: 03-28-2008, 08:46 PM
  3. Peculiarty in code of simple program...
    By Kreuz14 in forum New To Java
    Replies: 4
    Last Post: 01-23-2008, 03:27 AM
  4. help with simple program in java
    By katie in forum New To Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM
  5. help with simple java program
    By leonard in forum New To Java
    Replies: 3
    Last Post: 07-30-2007, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •