Results 1 to 5 of 5
  1. #1
    JBeese is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default 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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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);

  3. #3
    JBeese is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    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.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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)

  5. #5
    JBeese is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    alright thanks, that worked great! Now only 2 more parts to write and i can be done with this one. This programing stuff is difficult.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •