• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Quick stupid Java question

Status
Not open for further replies.

Ecrofirt

Member
OK, here's the description of the program I've got to do:
A large company pats its salespeople on a commission basis. The salespeople recieve $200 per week, plus 9% of their gross sales for that week. For example, a salesperson who sells $5000 worth of merchandise in a week recieves $200 plus 9% of $5000, or a total of $650. You have been supplied with a list of items sold by salespeople. The values of these items are as follows:

1) 239.99
2) 129.75
3) 99.95
4) 350.89

Develop a Java application that inputs one salesperson's items sold for last week and calculates and displays that salesperson's earnings. There is no limit to the number of items that can be sold by a salesperson.


---

OK, the program itself isn't hard. I figure I've got the code just about perfect, but I'm getting an error that looks more like a warning, but won't let the program run:

Java:
import javax.swing.JOptionPane;
public class ProductSums {
    
    public static void main(String[] args) {
        
        float sumSoFar=0,total=0;
        
        String numSold;
        
        int productNum=1;
        int sold=0;
        
        while (productNum<=4){
            numSold= JOptionPane.showInputDialog("Enter the quantity of product " + productNum
            + " sold:");
            
            sold=Integer.parseInt(numSold);
            
            if (productNum==1){
                sumSoFar=sumSoFar + (sold*239.99);
            }else if (productNum==2){
                sumSoFar=sumSoFar + (sold*129.75);
            }else if (productNum==2){
                sumSoFar=sumSoFar + (sold*99.95);
            }else{
                sumSoFar=sumSoFar + (sold*350.89);
            }
            
            productNum+=1;
                            
        }
        
        total=200+ (.09*sumSoFar);
        
        JOptionPane.showMessageDialog(null,"" + total);
        
            System.exit(0);
        
    }
    
}

The error I'm getting occurs with each time i do an equation. It is as follows:

Code:
ProductSums.java [33:1] possible loss of precision
found   : double
required: float
        total=200 + (.09*sumSoFar);
                  ^
 

Phoenix

Member
Casts the doubles to ints, the type safety rules in Java are going to be different than what you are assuming.

total=200 + (.09*sumSoFar);

total=200 + (float)(.09*sumSoFar);


By default unspecified floating point literals are considered doubled, not floats.
 

Ecrofirt

Member
if i do that, won't it turn all that math into an integer, and then lose precision?

What I'm looking to do is keep the precision of the math without it being rounded off as an integer.

Of course, this then presents the problem of my needing to have the answer displayed as a decimal answer with two digits after the decimal at the end, which I also can't seem to find anywhere in the reading that we've had so far in the book.
 

Ecrofirt

Member
Honestly, I don't know.

The book hasn't discusses anything like that, so I wouldn't know.

I tried changing the (int) to (float), and it seems to have worked.

Now how can I go about getting the precision down to two places after the decimal? The book definitely hasn't discussed that yet.
 

Phoenix

Member
Ecrofirt said:
Honestly, I don't know.

The book hasn't discusses anything like that, so I wouldn't know.

I tried changing the (int) to (float), and it seems to have worked.

Now how can I go about getting the precision down to two places after the decimal? The book definitely hasn't discussed that yet.


Yeah, Java considers all undeclared decimals to be doubles.

You could have also done a couple of other things:

* Make total a double.
* Mark the literal as a float by appending f to them, otherwise the compiler says they are doubles

By 'law' Java doesn't make doubles floats via implicit type casting (something that Java doesn't do).
 
Status
Not open for further replies.
Top Bottom