1

I was just given my first assignment in my Java class, and we were tasked with creating a program that counts nickles and displays the total amount of money. However, whenever I put an odd number of nickles it does not display the corrent amount of money. For example, 1 nickles turns into $.5 instead of $.05. 21 Nickles turns into $1.5 instead of $1.05. I'm asumming this is an easy fix for an easy problem, but I am finding myself stumped. Thanks for the help.

public static void main(String[] args) 
{
      Scanner in = new Scanner(System.in);

      int nickles;

      System.out.println("Deposit your Nickles.");
      nickles = in.nextInt();
      int nickles5 = nickles * 5;


      int dollars = nickles5 / 100;
      int change = nickles5 % 100;


      System.out.println("You have $" + dollars + "." + change);
}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Nyctrimly
  • 23
  • 3

3 Answers3

0

You need to format change to use 2 digits, rather than just printing out the raw number. Specifically, if change==5, you want to print out 05.

You can do this with String.format("%02d", change)

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86
0

You have a random apostrophe at the end, why? You want to use floats instead of ints, like this: Scanner in = new Scanner(System.in); int nickles;

    System.out.println("Deposit your Nickles.");
    nickles = in.nextInt();//get number
    float nickles5 = nickles * 5;//input 1, get 5
    float amount = nickles5/100;
    System.out.println("$"+ amount);

Output:

Deposit your Nickles.
1
$0.05

Deposit your Nickles.
21
$1.05

EDIT: Code with formatted output:

    Scanner in = new Scanner(System.in);
    int nickles;

    System.out.println("Deposit your Nickles.");
    nickles = in.nextInt();//get number
    float nickles5 = nickles * 5;//input 1, get 5
    float amount = nickles5/100;
    String output = String.format("%02f", amount);//f(loat) not d

    System.out.println("$"+ output);
ruyili
  • 694
  • 10
  • 25
  • [I would not recommend relying on `float` to do your number formatting for you](http://stackoverflow.com/questions/1661273/floating-point-arithmetic-not-producing-exact-results) – Krease Jan 26 '16 at 23:13
  • Then you may use `String.format` , as @Chris states. – ruyili Jan 26 '16 at 23:15
0

I'll go out on a limb here, to try to solve your problem. It appears that you will ask the user to enter some number of nickels, and you will output the amount in dollars. Please let me know if this is incorrect in any way.

Now look at the following code.

public static void main(String[] args) {
      Scanner in = new Scanner(System.in);

      int nickles;
      System.out.println("Deposit your Nickles.");
      nickles = in.nextInt();
      double nickles5 = nickles * 0.05;
      System.out.println("You have $" + nickles5);
}

After we have the number of nickels stored in an int, we multiply it by 0.05, to get the actual value of all the nickels. We store this in a double variable [Note: int multiplied to a double, returns a double; it is called numeric promotion]. Now, to get the total value, you can simply print this double variable. In a case where n=2, nickels5 = 0.1. So, it will print $0.1

If you want this to show up as $0.10, simply replace the nickels5 with String.format( "%.2f", nickels5)

Now your final line will look like:

System.out.println("You have $" + String.format( "%.2f", nickels5));

Let me know if this solved your issue.

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
  • NOTE: you can as easily replace the double with a float; I don't see it mattering much in your case. – Debosmit Ray Jan 27 '16 at 02:42
  • This definitely helped me with my issue, thank you very much. If you don't mind, could you explain how the String.format( "%.2f", works? Is it just telling it to only go up to two decimal places? – Nyctrimly Jan 27 '16 at 07:10
  • Precisely. If it was %f, it would imply that you want to print a float variable in there. Since, we have a 2 after a period, it tells the compiler that you want to print 2 places of decimal for a float value. – Debosmit Ray Jan 27 '16 at 23:00