Anyways, I'm having trouble with this Cash Register code. I've been working on it for a day now, and it still won't work. Any ideas?

import java.util.*;
import java.io.*;

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

        int amountDue,amountPaid;
        double pennies,dollars,nickels,dimes,quarters;

        System.out.print("The amount due: ");
        amountDue = console.nextInt();

        System.out.print("The amount paid: ");
        amountPaid = console.nextInt();

        System.out.print("You change is: ");

        pennies = amountPaid-amountDue+50;

        dollars=pennies/100;
        pennies=pennies%100;

        quarters = pennies/25;;
        pennies = pennies%25;

        dimes = pennies/10;
        pennies = pennies%10;

        nickels = pennies/5;
        pennies = pennies%5;

        pennies = pennies/1;
        pennies = pennies%1;

        System.out.println("The change due:");
        System.out.println(dollars + "dollars");
        System.out.println(quarters + "quarters");
        System.out.println(dimes + "dimes");
        System.out.println(nickels + "nickels");
        System.out.println(pennies + "pennies");
    }
}

it still won't work.

Can you explain what "won't work" means?
If there are errors, copy and paste the full text here.
If the results are wrong, copy and paste them here and explain what is wrong with them.

To see what the code is doing add some println statements to print out the results of each expression and variable that is given a value.,

Edited 4 Years Ago by NormR1

are you doing only integer?
Also, do you mean when you have change of 10
you wnat it to be presented as
10
100 cent
40 quar
1 dim
2 nic
?
or do u mean to use double instead of int?
when you pay 40 out of 25.25
you get 14.75?
which mean
14$ and 2 Quart + 1 dim + 1 nk?

? which one you are doing?

Both lines that state "amountPaid = console.nextInt()" error out. It will compile fine, but when I run the class file, it will error saying something about java.util.nextInt. It will only error though once I try to enter in the amount.

How did you enter when the question shows up? Do you understand what nextInt() mean?

When you use nextInt(), the input is expected to be an integer (i.e. 234, 123), not a decimal number (i.e. 25.45, 100.0). If your input is not followed the integer format, you will get an exception. I am quite sure that your input is in the decimal format (double or float), not an integer.

PS: When someone asks you for error message, you should copy the whole error message from Java because it contains all the information of what's going wrong from what class and at what line. Summarize the error yourself who does not know much about the language would be even worse, and as a result you may get a wrong answer for your wrong summary.

Edited 4 Years Ago by Taywin

it will error saying something about java.util.nextInt.

Please copy and paste here the full text of the command console showing the input and the error messages.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Cash.main(Cash.java:21)

This is the error. Sorry guys, just started coding 3 weeks ago.

Please copy and paste here the full text of the command console showing the input and the error messages.
Your post does NOT show what the program printed or what you typed in.

The error message says that the data being read by the nextInt() method on line 21 was not a valid integer.

Edited 4 Years Ago by NormR1

The Console

Cash.main({ });
The amount due: 2.83
    Exception occurred.

The Error:

java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Cash.main(Cash.java:21)

Like I said, sorry guys.

Edited 4 Years Ago by ToneZ: Edit

Then you should read what I said in my previous answer... It is exactly what happened -- the input is not an integer format but a float/double.

There are 2 ways to solve this issue -- one is to change the nextInt() to nextDouble() and the other is to accept the whole line using nextLine() and attempt to parse it to a number. Please refer to Scanner class API document.

Edited 4 Years Ago by Taywin

This article has been dead for over six months. Start a new discussion instead.