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

Thread: program : giving back change with dollars, dimes and pennies

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default program : giving back change with dollars, dimes and pennies

    When I run it , it wont print out the nickels and pennies.

    I was asked to only put the input in pennies...




    package givingChange;
     
    import javax.swing.JOptionPane;
     
    public class givingChange {
     
     
    	public static void main(String[] args) {
     
     
    		String amount;
    		int price, paid, change;
    		int dollars, quarters, dimes, pennies, nickels;
     
     
    		// In pennies
    		 amount=
    			 JOptionPane.showInputDialog("Enter amount due");
    		  price = Integer.parseInt(amount);
    		 amount=
    			 JOptionPane.showInputDialog("Enter amount paid");
    		 paid = Integer.parseInt(amount);
     
     
     
    		 change = paid - price;
     
     
    		 dollars = change/100;
    		 int left= change%100;
     
    		 quarters = left/25;
    		 left= left%25;
     
    		 dimes = left/10;
    		 left=left%10;
     
    		 nickels = left/5;
    		 left = left%5;
     
    		 pennies = left/1;
     
    		 if (dollars>0)
    		{ System.out.println(dollars+" dollars"); }
    		if (quarters>0)
    		 {System.out.println(quarters+ " quarters");}
    		 if (dimes>0)
    		 {System.out.println(dimes+" dimes");
    		 if  (nickels>0)
    		 {System.out.println(nickels+ " nickels");}
    		 if (pennies>0)
    		 {System.out.println(pennies+" pennies");}
     
     
    	}
     
    	}
    }
    Last edited by hellynam; September 24th, 2012 at 09:21 PM.


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

    Default Re: program : giving back change with dollars, dimes and pennies

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    it wont print out the nickels and pennies
    Their values must be 0. Check your code and logic to be sure your formulas are correct.

    Try debugging the code by printing out the values of the variables as they are changed. For example print out the value of left after every time it is assigned a new value. The print out will show you where your formulas are going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: program : giving back change with dollars, dimes and pennies

    Quote Originally Posted by hellynam View Post
    When I run it , it wont print out the nickels and pennies
    Maybe if you adopt a style with consistent indentation to emphasize control structures you might avoid certain problems like this.

    I mean, the compiler ignores these stylistic niceties, but for humans, there is a reason that experienced programmers often get in the habit of making their code look "neat" in a more-or-less standard form.

    1. Each block should start and end in the same column.

    2. Inner Block contents are indented more than outer blocks.

    3. For emphasis, sometimes people put comments at the closing '}' of some of the blocks.

    4. Judicious (and consistent) use of whitespace sometimes makes things more readable or at least more attractive...


    One style that seems to be popular with Java programmers might make your code look like the following:
    package givingChange;
     
    import javax.swing.JOptionPane;
     
    public class GivingChange {
     
        public static void main(String [] args) {
            String amount;
            int price, paid, change;
            int dollars, quarters, dimes, pennies, nickels;
     
            amount = JOptionPane.showInputDialog("Enter amount due");
            price = Integer.parseInt(amount);
     
            amount = JOptionPane.showInputDialog("Enter amount paid");
            paid = Integer.parseInt(amount);
     
            change = paid - price;
     
            dollars = change / 100;
            int left = change % 100;
     
            quarters = left / 25;
            left = left % 25;
     
            dimes = left / 10;
            left = left % 10;
     
            nickels = left / 5;
            left = left % 5;
     
            pennies = left / 1;
     
            if (dollars > 0) {
                System.out.println(dollars + " dollars");
            } // End of "if (dollars...)"
            if (quarters > 0) {
                System.out.println(quarters +  " quarters");
            } // End of "if (quarters...)"
            if (dimes > 0) {
                System.out.println(dimes + " dimes");
                if (nickels > 0) {
                    System.out.println(nickels + " nickels");
                }
                if (pennies > 0) {
                    System.out.println(pennies +  " pennies");
                }
            } // End of "if (dimes...)"
        } // End of main()
    } // End of class definition


    Cheers!

    Z
    Last edited by Zaphod_b; September 25th, 2012 at 12:40 PM.

Similar Threads

  1. [SOLVED] Help with my Java program: Making change from an entered double.
    By iDizzle in forum What's Wrong With My Code?
    Replies: 20
    Last Post: March 18th, 2012, 03:16 PM
  2. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 01:43 PM
  3. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM
  4. Question: Converting cents to dollars.
    By shamed in forum Java Theory & Questions
    Replies: 4
    Last Post: December 26th, 2009, 06:33 PM
  5. cents to dollars and cents program
    By roaster in forum Java Theory & Questions
    Replies: 3
    Last Post: October 24th, 2009, 12:56 AM