Thread: Java help, if possible

Results 1 to 2 of 2
  1. #1 Java help, if possible 
    Member PSP User
    Join Date
    Jul 2007
    I'm working on my first java program and I'm stuck. I have to write a code that takes a dollar amount due and subtracts from the dollar amount paid (ie, 3.82 due and 4.00 paid = 4.00-3.82), then I have display the amount of change.

    So, I'd have to calculate how many quarters, nickles, dimes and pennies. It's always less than $1 in change being given back.

    I have the math part figured out and most of the coding, I just cannot figure out how to put it together. I have to store the dollar amounts as double data types (3.82 and 4.00) but I need to display the change in integers. I cannot figure out how to go from double to integer.

    I did this same project in C and it was simple, but this part I just cannot figure out in java because it's keeping everything as double since the amount paid and amount owed are both double data types.

    I tried multiplying the change * 100 so at least I can use integers but I cannot store it as an integer so there are always decimals at the end and my answers return decimals (like 3.5 quarters or 4.22222 dimes, etc.)

    here's what I have so far:
    import java.util.Scanner;
    public class Assignment1
    	public static void main( String [] args )
    		Scanner scan = new Scanner( );
    		final double PENNIES = 1;
    		final double NICKLES = 5;
    		final double DIMES = 10;
    		final double QUARTERS = 25;
    		System.out.println( "Enter the total amount due> ");
    		double total = scan.nextDouble();
    		System.out.println( "Enter amount paid> ");
    		double paid = scan.nextDouble();
    		double change = paid - total;
    		change = change * 100;
    		double quartersChange = change / QUARTERS;
    		System.out.println( "You will receive " + quartersChange + " quarters" );
    It returns a value of 2.9600000000000013 quarters, but I want it to drop the remainder and just use "2" as the value.

    any help would be appreciated.
    Reply With Quote  

  2. #2  
    Member PSP User Leozin's Avatar
    Join Date
    Jul 2007

    There are a lot of ways to do that

    the first is: cast your result to an Integer

    instead of
    double quartersChange = change / QUARTERS;
    try to put
    int quartersChange = (int) (change / QUARTERS);

    the second is: get the floor/ceil of your number, like that:
    double quartersChange = Math.floor( change / QUARTERS );
    double quartersChange = Math.ceil( change / QUARTERS );

    you can make a BigDecimal object and format its scale, like that:
    BigDecimal myNumber = new BigDecimal( change / QUARTERS );
    myNumber.setScale( 2 ); // with 2 decimal scales
    // I dunno if it really works, but try :P

    finally you can make a DecimalFormat
    DecimalFormat myFormater = new DecimalFormat( "0.00" );
    System.out.println( myFormater.format( change / QUARTERS );

    try these examples and tell me if all worked ok

    see ya dude
    Swinming in your dark rivers... Tristania ftw
    Don't have PSP anymore
    Powered by Sony Playstation 3 - 60gb =]
    Current games: Resident Evil 5, Heavenly Sword and Fifa Street
    Reply With Quote  

Similar Threads

  1. java
    By ILoveFrenchFries in forum PSP Discussion
    Replies: 6
    Last Post: 05-25-2009, 01:21 AM
  2. psp java
    By robby10ant in forum PSP Homebrew
    Replies: 4
    Last Post: 07-28-2008, 01:29 PM
  3. Java
    By MOOSE#51 in forum Linux
    Replies: 6
    Last Post: 04-29-2008, 02:19 PM
  4. Java Help
    By psp23 in forum Off-Topic Discussion
    Replies: 0
    Last Post: 11-25-2007, 01:51 AM
  5. Java
    By ZX83 in forum PSP Software, Firmware & Plugins
    Replies: 4
    Last Post: 11-09-2007, 07:41 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts