Results 1 to 5 of 5
- 06-04-2011, 12:33 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
maybe someone can help with this problem
public void getSecondInput()
{
try
{
priceOfItems = Integer.parseInt(priceOfItemsJTextField.getText()) ;
calculateOutput();
}
catch(NumberFormatException exception)
{
JOptionPane.showMessageDialog(this,
"Please enter Price per Item!",
"Number Format Error", JOptionPane.ERROR_MESSAGE );
priceOfItemsJTextField.setText("");
priceOfItemsJTextField.requestFocusInWindow();
}
}
public void calculateOutput()
{
subTotal = calculateSum(numberOfItems, priceOfItems);
displayOutput();
}
public int calculateSum(int valueOne, int valueTwo)
{
return valueOne * valueTwo;
}
public void displayOutput()
//priceOfItems = Integer.parseInt(priceOfItemsJTextField.getText()) ;
calculateOutput();// I think this might be wrong.
The problem im having is in writing the code for multiplying the numberOfItems which is an integer and the priceOfItems which is a double, i get an error code saying this.
RetailSales.java:250: calculateSum(int,int) in RetailSales cannot be applied to (int,double)
subTotal = calculateSum(numberOfItems, priceOfItems);
I have a feeling my priceOfItems code is wrong because its a double, but the book im using really isnt any help, and ive tried everything i can think of. If i change priceOfItems to an Int up at the declaration section, the program works fine. any ideas?
- 06-04-2011, 12:40 AM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
For future reference, please wrap your code within the code tags.
Depending upon what your requirements are, you could change the method declaration to accept a double, overload the method declaration to accept different types of parameters, or cast the double to an int
Java Code:calculateSum(numberOfItems, (int)priceOfItems);
- 06-04-2011, 01:06 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0
sorry will use code tag from now, That fixed the error message and now the program runs. However now when run the program and try to set the price at something with a decimal like 1.85 the catch error pops up telling me to enter a price. if i use a whole number it works fine.
- 06-04-2011, 01:12 AM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
You are using Integer.parseInt, which expects a String representation of the integer - it can't parse floating points. Use Double.parseDouble to parse floating points (and integer values) - but if you enter a double you will loose all precision if you cast to an int for the method you posted (hence my other suggestions above)
- 06-04-2011, 01:21 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 10
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks