# First timer here! Help with very very basic problem

Printable View

• January 31st, 2014, 12:12 PM
brobertson300
First timer here! Help with very very basic problem
Hello my name is Brandon and I am taking intro to Java for the first time and I do not have and previous experience with programming.

I am stuck on a HW problem:

1) Sales Prediction - The East Coast sales division of a company generates 62 percent of total sales. Based on that percentage, write a program that will predict how much the East Coast division will generate if the company has \$4.6 million in sales this year.

The basic program I wrote seems to not be working and could use some help.

public class SalesPrediction
{
public static void main(String[] args)
{
int x = 4600000;
int y = 0.62;

int total = x * y;

System.out.println("The East Coast will generate \$ " + total + " this year. ");
}
}

The error I am getting says possible loss of precision.

Again I am a complete beginner so any help would be great! Thanks in advance :cool:
• January 31st, 2014, 12:17 PM
aussiemcgr
Re: First timer here! Help with very very basic problem
int y = 0.62;
Is not valid. An integer (int) cannot contain a decimal number. If you want to use a decimal number, you need to use a double
double y = 0.62;

Also, your int total needs to be a double total, or the fractional part of the x * y equation will be lost.
• January 31st, 2014, 12:21 PM
brobertson300
Re: First timer here! Help with very very basic problem
Thank you so much! Do you think this program is valid; would you have done it another way? Im trying to learn as much as possible and just pick peoples brain.
• January 31st, 2014, 12:29 PM
GregBrannon
Re: First timer here! Help with very very basic problem
Welcome to the Forum! Please read this topic to learn how to post your code correctly along with other useful info for newcomers.
• January 31st, 2014, 12:44 PM
aussiemcgr
Re: First timer here! Help with very very basic problem
For your purposes, it seems fine. If you wanted to "go the extra mile" and pretty up the output a bit, you could format the total appropriately before you print it. By that, I mean you could make it so only the first two decimal places are displayed (since that is usually how you would see money displayed). You can do that quite easily with the NumberFormat class.
Here is a brief example of how it works:
Code java:

```double money = 100.25789; NumberFormat formatter = NumberFormat.getCurrencyInstance(); String moneyString = formatter.format(money); System.out.println("The money amount is "+moneyString); // This will print out: The money amount is \$100.26```

Another common practice, is not using double for money. Since you are probably not dealing with "real" money, it isn't an issue. But in real-world applications where money precision is important (like an application for a Bank), the BigDecimal class is recommended by most people. This is because there can be small mathematical mistakes with doubles due to something called "floating-point arithmetic" and because the computer cannot always accurately store a base 10 decimal number as bits. An example of this error can be seen if you run this basic code:
Code java:

```double number = 0.1 * 0.1; System.out.println(number);```
We know that the answer to that should be 0.01, but due to how the computer stores the number "0.1", the computer will give the result: 0.010000000000000002
It's just a fun little fact you might see on an exam or something some day, lol.