Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Thumbs down Getting the remaining cents

    Hi, I would like to know how to get the remaining cents from the program I've made..

    Here's the program:

    Java Code:
    import java.io.*;
    
    public class centsDollars {
    
        public static void main (String[]args) {
            
            BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
            
            String x="";
            
            try {
                System.out.println("Enter amount in nickel: ");
                x = input.readLine();
                int nickelin = Integer.parseInt(x);
                
                System.out.println("Enter amount in penny: ");
                x = input.readLine();
                int penniesin = Integer.parseInt(x);
    
                double pennies = (nickelin*5)+(penniesin);
                double nickel = (pennies/5);
                
                System.out.println("You entered " +nickelin+ " nickels and " +penniesin+ " pennies.\n");
                System.out.println("The total amount in penny is "+pennies+".");
                System.out.println("The total amount in nickel is "+nickel+".\n");
                double dollar = (pennies/100);
                System.out.println("The total amount is " +dollar+" Dollar." );
            }
            
            catch (IOException e) {
                System.out.println("Error!");
            }
        }
        
    }

    Assuming I entered 3 nickels and 122 pennies.

    This displays:

    You entered 3 nickels and 122 pennies.


    The total amount in penny is 137.0.

    The total amount in nickel is 27.4.

    The total amount is 1.37 Dollar.

    How can I output the amount in dollars the same as this?:
    The total amount is 1 Dollar and 37 Cents.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Forget using double. Everything here is countable so use int and reserve double for things that you would measure rather than count.

    Find out about the integer division and the remainder (%) operator. Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    Hi, thanks for your reply.

    Sorry, sir. I don't get it :(

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I don't get it

    Is there something specific in the tutorial section that you don't understand? Or can you not see how to apply the / and % operators to your problem?

    Java Code:
    public class Cents {
        public static void main(String[] args) {
            int test = 12345;
            
            System.out.println("Suppose I have " + test + " cents");
            System.out.print("That would be " + (test / 100) + " dollars ");
            System.out.print("and " + (test % 100) + " cents");
        }
    }

  5. #5
    Join Date
    Dec 2009
    Posts
    10
    Rep Power
    0

    Default

    Hi sir. I did not know how to apply the % operator, sorry. It works fine now. Thank you very much for your help.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

Posting Permissions

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