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

Thread: Program trying to calculate money, won't work

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Program trying to calculate money, won't work

    My program is supposed to ask the user to enter a dollar value from up to 100 dollars, and output the amount of bills and coins required to create that amount with the least change possible. I'm not completely finished, however when I test run the program and I enter values below 50, it doesnt seem to give me wrong info and negative numbers. Why is this happening? Thanks

    import java.io.*;
     
    public class ChangeProgram
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));
     
        String inputA; 
        double inputB = 0.0, input = 0.0, remainder = 0, remainderB = 0, remainderD = 0, remainderF = 0, remainderH = 0, remainderJ = 0, remainderL = 0, remainderN = 0, remainderP = 0, remainderR = 0;
     
     
        System.out.println("Please enter a dollar amount: ");
        inputA = myInput.readLine();
        input = Double.parseDouble(inputA);
     
        System.out.println("\nAmount: \n");
     
        if (input >= 50 || input < 100){
          double answer = input / 50;
          double fifty = Math.floor(answer);
     
          System.out.println(fifty + " $50 bill(s)");
          remainder = input - 50;
        }
     
          if(remainder >= 20 || remainder < 50){
            double answerB = remainder / 20;
            double remainderA = Math.floor(answerB);
     
            System.out.println(remainderA + " $20 bill(s)");
     
            double twenty = remainderA * 20;
            remainderB = remainder - twenty;
          }
     
     
         if(remainderB >= 10 || remainderB < 20){
            double answerC = remainderB / 10;
            double remainderC = Math.floor(answerC);
     
            System.out.println(remainderC + " $10 bill(s)");
     
            double ten = remainderC * 10;
            remainderD = remainderB - ten;
           }
     
          if(remainderD >= 5 || remainderD < 10){
            double answerD = remainderD / 5;
            double remainderE = Math.floor(answerD);
     
            System.out.println(remainderE + " $5 bill(s)");
     
            double five = remainderE * 5;
            remainderF = remainderD - five;
            }
     
          if(remainderF >= 2 || remainderF < 5){
            double answerE = remainderF / 2;
            double remainderG = Math.floor(answerE);
     
            System.out.println(remainderG + " toonie(s)");
     
            double two = remainderG * 2;
            remainderH = remainderF - two;
           }
     
         if(remainderH >= 1 || remainderH < 2){
            double answerF = remainderH / 1;
            double remainderI = Math.floor(answerF);
     
            System.out.println(remainderI + " loonie(s)");
     
     
            double one = remainderI * 1;
            remainderJ = remainderH - one;
     
         }
     
        if (remainderJ >= 0.25 || remainderJ < 1){
            double answerG = remainderJ / 0.25;
            double remainderK = Math.floor(answerG);
     
            System.out.println(remainderK + " quarter(s)");
     
            double quarter = remainderK * 0.25;
            remainderL = remainderJ - quarter;
     
          }
     
     
        if(remainderL >= 0.10 || remainderL < 0.25){
            double answerH = remainderL / 0.10;
            double remainderM = Math.floor(answerH);
     
            System.out.println(remainderM + " dime(s)");
     
            double dime = remainderM * 0.10;
            remainderN = remainderL - dime;
              }
     
          if(remainderN >= 0.05 || remainderN < 0.10){
            double answerI = remainderN / 0.10;
            double remainderO = Math.floor(answerI);
     
            System.out.println(remainderO + " nickel(s)");
     
            double nickel = remainderO * 0.10;
            remainderP = remainderN - nickel;
              }
     
         if (remainderP >= 0.01 || remainderP < 0.05){
            double answerJ = remainderP / 0.01;
            double remainderQ = Math.floor(answerJ);
     
            System.out.println(remainderQ + " penny(ies)");
     
            double penny = remainderQ * 0.01;
            remainderR = remainderP - penny;
              }
     
     
     
     
     
     
     
    }
     
    }
    Last edited by jps; September 4th, 2013 at 11:58 PM. Reason: code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Program trying to calculate money, won't work

    Time for some debugging.
    Step through the code yourself with a test value and see what happens as the program executes

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Program trying to calculate money, won't work

    I went through the code, added some thing and fixed most of the problem. However when I enter certain amounts, such as numbers ending with 3 (e.x 85.23) it gives me the wrong amount of change. Do you know why this is happening?

    HTML Code:
    import java.io.*;
    
    public class ChangeProgram
    {
      public static void main (String [] args) throws IOException
      {
        BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));
        
        String inputA; //String Variable is declared
        double input = 0; //Double variable is declared and set to zero
        
        
        try {//try statement
          
          System.out.println("Please enter a dollar amount or press '999' to exit: ");
          inputA = myInput.readLine();//reads user input
          input = Double.parseDouble(inputA);//coverts string into a double
          if (input <= 0 || input > 100) {
            throw new NumberFormatException();
          }
        } catch (NumberFormatException e) { // catches any characters entered as the radius
          System.err.println("Invalid number entered!");// error output sentence
          System.exit(0);
        }//end catch
        
        while (input != 0){
          
          if (input == 100){
            System.out.println("1 $100 bill");
          }
          
          if (input >= 50 || input < 100){//run, if amount is between 50 and 100
            double answer = input / 50;//divide amount by 50
            double fifty = Math.floor(answer);//round amount down to nearest whole number
            
            System.out.println(fifty + " $50 bill(s)"); //print # of bills
            double fiftyA = fifty * 50;//find the remainder
            input = input - fiftyA;
          }
          
          if(input >= 20 || input < 50){//run if amount is between 20 and 50
            double answerB = input / 20;//divide amount by 20
            double remainderA = Math.floor(answerB);//round amount down to nearset whole #
            
            System.out.println(remainderA + " $20 bill(s)");//print # of bills
            
            double twenty = remainderA * 20;//find the remainder
            input = input - twenty;
          }
          
          if(input >= 10 || input < 20){//run if amount is between 10 and 20
            double answerC = input / 10;//finds # of bills 
            double remainderC = Math.floor(answerC);//round # of bills down to nearest whole #
            
            System.out.println(remainderC + " $10 bill(s)");//print # of bills
            
            double ten = remainderC * 10;//finds remainder
            input = input - ten;
          }
          
          if(input >= 5 || input < 10){//run if amount is between 5 and 10
            double answerD = input / 5;//finds # of bills
            double remainderE = Math.floor(answerD);//round number of bills down to nearest whole #
            
            System.out.println(remainderE + " $5 bill(s)");//print # of bills
            
          double five = remainderE * 5;//finds the remainder
          input = input - five;
        }
        
        if(input >= 2 || input < 5){//run if amount is between 2 and 5
          double answerE = input / 2;//finds # of coins
          double remainderG = Math.floor(answerE);//rounds down to the nearest whole #
          
          System.out.println(remainderG + " toonie(s)");//print # of coins
          
          double two = remainderG * 2;//finds the remainder
          input = input - two;
        }
        
        if(input >= 1 || input < 2){//run if amount is between 1 and 2
          double answerF = input / 1;//finds # of coins
          double remainderI = Math.floor(answerF);//rounds down to nearest whole #
          
          System.out.println(remainderI + " loonie(s)");//prints # of coins
          
          
          double one = remainderI * 1;//finds remainder
          input = input - one;   
        }
        
        if (input >= 0.25 || input < 1){//run if amount is between 0.25 and 1
          double answerG = input / 0.25;//finds # of coins
          double remainderK = Math.floor(answerG);//rounds down to the nearest whole #
          
          System.out.println(remainderK + " quarter(s)");//prints # of coins
          
          double quarter = remainderK * 0.25;//finds remainder
          input = input - quarter;
          
        }
        
        
        if(input >= 0.10 || input < 0.25){//run if amount is between 0.10 and 0.25
          double answerH = input / 0.10;//finds # of coins
          double remainderM = Math.floor(answerH);//rounds down to the nearest whole #
          
          System.out.println(remainderM + " dime(s)");//prints # of coins
          
          double dime = remainderM * 0.10;//finds remainder
          input = input - dime;
        }
        
        if(input >= 0.05 || input < 0.10){//run if amount is between 0.05 and 0.10
          double answerI = input / 0.10;//finds # of coins
          double remainderO = Math.floor(answerI);//rounds down to nearest whole #
          
          System.out.println(remainderO + " nickel(s)");//prints # of coins
          
          double nickel = remainderO * 0.10;//finds remainder
          input = input - nickel;
        }
        
        if (input >= 0.01 || input < 0.05){//run if amount is between 0.01 and 0.05
          double answerJ = input / 0.01;//finds # of coins
          double remainderQ = Math.floor(answerJ);//rounds down to nearest whole #
          
          System.out.println(remainderQ + " penny(ies)");//prints # of coins
          
        }
        
          try {//try statement
          
          System.out.println("Please enter a dollar amount or press '999' to exit: ");
          inputA = myInput.readLine();//reads user input
          input = Double.parseDouble(inputA);//coverts string into a double
          if (input < 0 || input > 100 || input == 999) {
            throw new NumberFormatException();
          }
        } catch (NumberFormatException e) { // catches any characters entered as the radius
          System.err.println("Invalid number entered!");// error output sentence
          System.exit(0);
        }//end catch
        }//end while
       
      }//main method
      
    }//ChangeProgram

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Program trying to calculate money, won't work

    See the Announcements page for the proper use of code tags, obviously you are not pasting HTML code, why use the HTML tag?

    As for your question, it seems to be the same question as before, so my reply is the same as before.
    Time for some debugging...

Similar Threads

  1. do-while loop won't work
    By ncampbell605 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 12th, 2012, 10:07 AM
  2. Image won't work!
    By mkrage in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2012, 05:08 PM
  3. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  4. Program won't work on all operation systems
    By 123099 in forum AWT / Java Swing
    Replies: 8
    Last Post: July 18th, 2011, 05:11 PM
  5. MergeSort won't work
    By joshft91 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 09:44 PM