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 2 of 2

Thread: change finder

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default change finder

    hey guys below is my code for a change finder the code takes in a sales total and the amount payed and gives a change value and the amount of coins without using nickles. i cant seem to figure out however why the code is one cent short on
    Given $36.0 for a purchase of $32.27 we need
    3 dollars 2 quarters 2 dimes 2 cents

    it should be 3 cents not 2 it is right on my other tests however.
    here is my code

    import java.util.Scanner;
    public class Change {
    public static void main(String [] args) {
    double purchase;
    double pay;
    double change;
    int balance;
    int dollars;
    int quarters;
    int dimes;
    int cents;
     
    Scanner in = new Scanner(System.in);
    System.out.print("purchase");
    purchase = in.nextDouble();
    System.out.println("payment");
    pay = in.nextDouble();
     
    change = pay - purchase;
    balance = (int)(change * 100);
    System.out.println(balance);      
    dollars = balance / 100;
    balance = (balance - (100 * dollars));
    quarters = balance / 25;
    balance = (balance - (25 * quarters));
    dimes = balance / 10;
    balance = (balance - (10 * dimes));
    cents = balance / 1;
    balance = balance - (1 * cents);
     
     
     
    System.out.println("Given $" + pay + " for a purchase of $" + purchase + " we need");
    System.out.println(dollars + " dollars " + quarters + " quarters " + dimes + " dimes " + cents + " cents");
     
    }
    }

    any help is appreciated i am stuck as to how to fix it. thanks i look forward to any suggestions.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: change finder

    The code is not properly formatted making it hard to read and understand. All statements should not start in the first column. Nested statements should be indented 3-4 spaces.

    Print out the value of change to see what the problem is. There is a problem working with doubles with integer like values. There is a web page somewhere that explains the rounding/precision problems with using doubles for currency computations.
    I got this from google:
    http://docs.oracle.com/cd/E19957-01/..._goldberg.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problems with my prime finder program
    By Truck35 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 9th, 2012, 12:56 PM
  2. Problem with my prime number finder program
    By Truck35 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 6th, 2012, 11:25 PM
  3. Fixing this grade finder program?
    By Stinger25 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: December 4th, 2012, 08:10 AM
  4. Help with my Range Finder programme
    By kidza12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 01:53 PM
  5. [SOLVED] Recursive Sentence Finder
    By raphytaffy in forum Algorithms & Recursion
    Replies: 4
    Last Post: February 21st, 2010, 02:46 PM