2

So I have Been working on a Program and it has been awhile since I have last used java. I was wondering how to get my program to accept decimals. I have tried looking it up but I couldn't find anything helpful and anything I really understood.Below is what I have done so far...

    package test;
    import java.util.Scanner;

    /**
     *
     * @author Thao
     */
    public class Test {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    // create Scanner to obtain input from command window
    Scanner input = new Scanner( System.in );
    double cash; // double makes it allow decimals
    int hundred;
    int twenty;
    int ten;
    int toonies ;
    int lonies;
    int quarters;
    int dimes;
    int nickels;
    int pennies;
    int money;
    int change;

    System.out.println();// blank line
    System.out.print("Hello ");
    System.out.println();
    System.out.println("Please enter a number with no decimal places ");
    cash = input.nextInt(); // read first integer
    System.out.println("you have Entered "+cash);
    hundred = 100; 
    change = (int)hundred - (int)cash; 
    System.out.println("The change from $100.00 is:" + (double)change);


    change = 100 * change; // multiply  cash by 100
    money = (int)change; //cash is now a int

     twenty = money / 2000; 
     money = money - (twenty * 2000); 

    toonies = money / 200; // money is multiplied by 100 than / by 200
     money = money - (toonies * 200); // ex. money =  1586 - (7.93 * 200 )

    lonies = money / 100 ; 
     money = money - (lonies * 100);

    quarters = money / 25;
     money = money - (quarters * 25);

    dimes = money / 10;
     money = money - (dimes * 10);

    nickels =  money / 5; 
     money = money - (nickels * 5);

    pennies = money / 1;
     money = money - (pennies * 1);
    if(twenty>0){
    System.out.println();
    System.out.print("You will have this many Twenties ");
    System.out.print(twenty + "."); 
    }
    else{
    }
    if(toonies>0){
    System.out.println();
    System.out.print("You will have this many Toonies ");
    System.out.print(toonies + ".");
    System.out.println();
    }
    else{
    }
    if(lonies>0){
    System.out.print(" You will have this many Lonies ");
    System.out.print(lonies + ".");
    System.out.println();
    }
    else{
    }
    if(quarters>0){
    System.out.print(" You will have this many quarters ");
    System.out.print(quarters + ".");
    System.out.println();
    }
    else{
    }

    if(dimes>0){
    System.out.print(" You will have this many dimes ");
    System.out.print(dimes + ".");
    System.out.println();
    }
    else{
    }

    if(dimes>0){
    System.out.print(" You will have this many nickels ");
    System.out.print(nickels);
    System.out.println();
    }
    else{
    }
    if(pennies>0){
    System.out.print(" You will have this many pennies ");
    System.out.print(pennies);
    System.out.println();
    }
    else{
    }
    System.out.println();
    System.out.println("Thank you for using my app ");


    }
    }
user3215990
  • 53
  • 1
  • 1
  • 11

1 Answers1

3

You can use double or float for the variable's data type. To read a double from your input you would do:

double cash = input.nextDouble();

or

float cash = input.nextFloat();
haley
  • 1,165
  • 7
  • 13
  • 2
    As a note of warning, doing anything involving real-world finance (i.e. actual money and not a classroom project) where precision is required [should consider use of BigDecimal](http://stackoverflow.com/questions/6320209/javawhy-should-we-use-bigdecimal-instead-of-double-in-the-real-world). – Compass Jan 23 '15 at 19:49