Skip to main content

Strange results adding two doubles

1 reply [Last post]
killingthemonkey
Offline
Joined: 2012-02-14
Points: 0

First, I am really new to Java. So, thanks for any help. Second, I put this in this forum because I didn't see a "N00bs Post Here" forum.

I am trying to do this excercise from a book. What happens is that instead of adding the monthlyRate and tax variables, total ends up bein the two numeric figures printed out together. In this case: 29.953.74375. I'd rather have it do the math.

Thanks for your help.

public class CellCalc {

public static void main(String[] args) {

double monthlyRate = 29.95;
int monthlyMin = 300;
int minUsed = 295;
double overageRate = .45;
double taxRate = .125;
double overageCharge = 0.00;
double total;
double tax;

System.out.println("Your monthly charge is " + monthlyRate + " for " + monthlyMin + ".\n");
System.out.println("This month you used " + minUsed + ".\n");

if (minUsed < monthlyMin){
System.out.println("You incurred no(0) overage.\n\n");

tax = (monthlyRate * taxRate);
total = (monthlyRate + tax);
System.out.println("Your total is " + monthlyRate + tax + ".");
}

}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
killingthemonkey
Offline
Joined: 2012-02-14
Points: 0

Never mind. Logic error, or rather lack of logic error.

Changed "System.out.println("Your total is " + monthlyRate + tax + ".");"

to

System.out.println("Your total is " + total + ".");