Reputation: 3
I have tried to do a simple math calculation for a formula involving variables. However, an error pops up in the compiler indicating that the variable types don't match. I have tried casting and changing the variable types, but they do not work. How do I fix this without destroying the basic format of my code.
I am not experienced at Java, so any pointers will help.
Here is the code. It is for a money conversion program. The error is in the second part of the code, the else part.
import java.util.Scanner;
public class MConvert
{
public static void main (String[] args)
{
int penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar, twentyDollar, fiftyDollar, hundredDollar;
//can sub $ sign for dollar in variable, convention otherwise
double totalMoney;
Scanner scan = new Scanner (System.in);
System.out.println ("Are you converting to the total? If so, type true. \nIf you are converting from the total, then type false.");
boolean TotalorNot = true;
TotalorNot = scan.nextBoolean();
if (TotalorNot) {
System.out.println ("Type in the number of one-hundred dollar bills.");
hundredDollar = scan.nextInt();
System.out.println ("Type in the number of fifty dollar bills.");
fiftyDollar = scan.nextInt();
System.out.println ("Type in the number of twenty dollar bills.");
twentyDollar = scan.nextInt();
System.out.println ("Type in the number of ten dollar bills.");
tenDollar = scan.nextInt();
System.out.println ("Type in the number of five dollar bills.");
fiveDollar = scan.nextInt();
System.out.println ("Type in the number of one dollar bills or coins.");
dollar = scan.nextInt();
System.out.println ("Type in the number of half-dollar coins.");
halfDollar = scan.nextInt();
System.out.println ("Type in the number of quarter-dollar coins.");
quarter = scan.nextInt();
System.out.println ("Type in the number of dimes.");
dime = scan.nextInt();
System.out.println ("Type in the number of nickels.");
nickel = scan.nextInt();
System.out.println ("Type in the number of pennies coins.");
penny = scan.nextInt();
totalMoney = (hundredDollar * 100) + (fiftyDollar * 50) + (twentyDollar * 20) + (tenDollar * 10) + (fiveDollar * 5) + (dollar * 1) + ((double)halfDollar * 0.5) + ((double)quarter * 0.25) + ((double)dime * 0.1) + ((double)nickel * 0.05) + ((double)penny * 0.01);
System.out.println ("Here is total monetary value of the bills and coins you entered: " + totalMoney);
} else {
System.out.println ("Type in the total monetary value:");
totalMoney = scan.nextDouble();
hundredDollar = ((int)totalMoney / 100);
fiftyDollar = ((int)totalMoney - (hundredDollar * 100)) / 50;
twentyDollar = ((int)totalMoney - (fiftyDollar * 50)) / 20;
tenDollar = ((int)totalMoney - (twentyDollar * 20)) / 10;
fiveDollar = ((int)totalMoney - (tenDollar * 10)) / 5;
dollar = ((int)totalMoney - (fiveDollar * 5)) / 1;
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
quarter = ((int)totalMoney - (halfDollar * 0.5)) / 0.25;
dime = ((int)totalMoney - (quarter * 0.25)) / 0.1;
nickel = ((int)totalMoney - (dime * 0.1)) / 0.05;
penny = ((int)totalMoney - (nickel * 0.05)) / 0.01;
System.out.println (hundredDollar + " hundred dollar bills");
System.out.println (fiftyDollar + " fifty dollar bills");
System.out.println (twentyDollar + " twenty dollar bills");
System.out.println (tenDollar + " ten dollar bills");
System.out.println (fiveDollar + " five dollar bills");
System.out.println (dollar + " one dollar bills or coins");
System.out.println (halfDollar + " half-dollar coins");
System.out.println (quarter + " quarter-dollar coins");
System.out.println (dime + " dimes");
System.out.println (nickel + " nickel");
System.out.println (penny + " penny");
}
}
}
Upvotes: 0
Views: 292
Reputation: 3794
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
Is entirely wrong , first remove this instruction.
Also change your code with below lines.
quarter = (int)((totalMoney - (halfDollar * 0.5)) / 0.25);
dime = (int) ((totalMoney - (quarter * 0.25)) / 0.1);
nickel =(int)( (totalMoney - (dime * 0.1)) / 0.05);
penny = (int)((totalMoney - (nickel * 0.05)) / 0.01);
You also need to improve logic of your code, I can clearly see it do not serve what you want to achieve.
Upvotes: 1
Reputation: 45060
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
What are you trying to achieve with the double cast on the halfDollar. Remove that first. It took me 5 mins to find out why there were so many red lines in your code.
Next, you can do a simple case like this:-
halfDollar = (int)((totalMoney - (dollar * 1)) / 0.5);
But its not advisable to cast double values to int as you may tend to lose a lot of decimal data. Why not simple have all penny, nickel, dime, quarter, halfDollar, dollar, fiveDollar, tenDollar,twentyDollar, fiftyDollar, hundredDollar;
as double
itself?
Upvotes: 0
Reputation: 24444
The problem is in the following line:
(double) halfDollar = (totalMoney - (dollar * 1)) / 0.5;
What is the meaning of the (double)
here? You cannot cast the left side of a variable assignment (the variable itself) to a double. The variable is declared to be an int, and that cannot be changed. Never.
This line compiles (but may be incorrect concerning semantics):
halfDollar = (int) ((totalMoney - (dollar * 1)) / 0.5);
Apart from that: I don't think, the program does, what you want it to do...
Upvotes: 0
Reputation: 198
At this line ((int)totalMoney - (tenDollar * 10)) / 5;
the cast is applied to just totalMoney
to cast the value of whole expression you must do this
(int)(totalMoney - (tenDollar * 10)) / 5);
Upvotes: 0