this is my problem!
I have to create a console application that asks for numbers of quarters, dimes, nickesl,and pennies.
I have to use the Scanner class that read in the integer values for each coin.
Calculate total values in cents.
Calculate number of dollars and remaining cents.

import java.util.Scanner;
public class MoneyScanner1 {
	
	public static void main( String [] args){
	

		int quarter =((100/100)*25);
		int dimes = ((100/100)*10);
		int nickels = ((100/100)*5);
		int pennies = ((100/100)*1);
		int dollars = (quarter+dimes+nickels+pennies);
		int cent = (100/1);
		Scanner scan = new Scanner (System.in);
		System.out.print("How many quarters?\t ");
		quarter = scan.nextInt();
		System.out.print("\nHow many dimes?  ");
		dimes = scan.nextInt();
		System.out.print("\nHow many nickels?  ");
		nickels = scan.nextInt();
		System.out.print("\nHow many pennies?  ");
		pennies = scan.nextInt();
		System.out.print("Your change equals" + dollars + "dolars" + "and" + cent + "cents"); 

	}
	

}

the output should look like this:Sample output

/* ===========================================

OUTPUT: 

How many quarters? 4 

How many dimes? 5 

How many nickels? 6 

How many pennies? 7 

Your change equals 1 dollars and 87 cents

=========================================== */

Dani AI

Generated

Short version: treat the four variables as counts (how many coins), read those counts first, then compute the total cents and convert that total into dollars and remaining cents. The original code initializes variables with per-coin values and then overwrites them with user input, which throws away the coin values. was right to say you should multiply the input counts by the coin values, and was right that the calculation belongs after reading input.

Example implementation (reads counts, computes total cents, then dollars and cents):

import java.util.Scanner;

public class MoneyScannerFix {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("How many quarters? ");
        int quarters = sc.nextInt();
        System.out.print("How many dimes? ");
        int dimes = sc.nextInt();
        System.out.print("How many nickels? ");
        int nickels = sc.nextInt();
        System.out.print("How many pennies? ");
        int pennies = sc.nextInt();

        int totalCents = quarters * 25 + dimes * 10 + nickels * 5 + pennies;
        int dollars = totalCents / 100;
        int cents = totalCents % 100;

        System.out.println("Your change equals " + dollars + " dollars and " + cents + " cents");
        sc.close();
    }
}

Notes and quick troubleshooting:

  • Use totalCents / 100 for whole dollars and totalCents % 100 for leftover cents; % gives the remainder.
  • Initialize variables as counts (or zero) — don’t preload them with coin values.
  • Validate input if needed (reject negative counts).
  • Closing the Scanner is good practice. This approach keeps the logic clear and avoids the overwrite bug in the original code.

Recommended Answers

All 4 Replies

... and what is your specific question?

What operation I do in order to get the total in dollars and cents? For example, this statement: Your change equals 1 dollars and 87 cents.

Member Avatar for Member #647493

Well...
Lines; 7, 8, 9, 10, and 12 don't make any sense.
It's fine to declare your "int"s, but don't bother to initialize them.
Line 11 should go before line 21.
BUT, the calculation is wrong.

You do not understand how a program works. This may be a bit of trouble to explain to you... OK, let me try by looking at your code inside the main() method.

// Declaration Part:
  // What you do here is to declare what variables and their type to be used.
  // You cannot use any variable before you declare in the program, or the
  // compiler will throw and error of "Symbol Not Found" or something like that.
  // Also each declaration, you may immediately initiate a value to the variable.
  // This is what you are doing down below - declare & initialize.
  // However, it contains worthless part of "100/100" which is equal to 1.
  // Anything multiply with 1 is equal to itself.
  int quarter =((100/100)*25);  // could be --> int quarter = 25;
  int dimes = ((100/100)*10);
  int nickels = ((100/100)*5);
  int pennies = ((100/100)*1);
  int dollars = (quarter+dimes+nickels+pennies);
  int cent = (100/1);

  // Now you are accepting input from a user.
  // You simply read in the number of quarters.
  // If you are going to keep your variable declaration as it is,
  // you need to change the way you compute here.
  Scanner scan = new Scanner (System.in);
  System.out.print("How many quarters?\t ");
  quarter = scan.nextInt();
  // Stop and think about it here...
  // If you simple assign an input value to the "quarter" variable,
  // whatever you have already initialized will be thrown away.
  // Now what you get in the "quarter" variable is what the user input
  // which is supposed to be a number of quarter!
  // What you need to do instead is to multiple the value of "quarter" with the
  // number of quarters which is the user input. Remember, assume that the
  // user is entering the right type of data - a number.

  // quarter = quarter * scan.nextInt();

  // After you changed your code, all others are the same way as you did with quarter.
  // Change them all please...
  System.out.print("\nHow many dimes?  ");
  dimes = scan.nextInt();
  System.out.print("\nHow many nickels?  ");
  nickels = scan.nextInt();
  System.out.print("\nHow many pennies?  ");
  pennies = scan.nextInt();

  // Now you are going to display the total.
  // If you simply display the total by adding all coins
  // (quarters, dimes, nickles, and pennies), you will get the total
  // in pennies, right?
  // But the problem is asking you to display it properly in dollars & pennies.
  // What you need to do is to take the result of whatever you get from
  // dividing the total amount of pennies by 100 as dollar (100 pennies = 1 dollar).
  // and the left over after you subtract the dollar amounts off the total
  // is the penny number.
  // so move your int dollar; down here...
  // int dollar = ...  // please do it yourself
  // int cent = ...  // please do it yourself
  System.out.print("Your change equals" + dollars + "dolars" + "and" + cent + "cents");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.