2

I am very to new to Java programming and I am taking a class based on Java. I am currently doing this coffee project which is based on boolean and RadioButtons. I believe I am almost done with it except I get this error message on the console. If there is any other mistakes please let me know so I can fix it!

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1 "
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)

    public void purchase()
    {
        //local variables
        String  quantityString, buttonString, nameString, coffeeType;

        float subTotalFloat, priceFloat, taxFloat, grandTotalFloat;
        int quantityInteger;

        //Format the values to currency format
        DecimalFormat valueDecimalFormat = new DecimalFormat("$#0.00");

        //retrieve the input from the  user
        nameString = nameTextField.getText();
        quantityString = quantityTextField.getText();
        buttonString = coffeeType ();

        //change data types
        quantityInteger = Integer.parseInt(quantityString);
        ...
    }
Mdev
  • 2,440
  • 2
  • 17
  • 25
Warren Xie
  • 21
  • 1
  • 1
  • 2

3 Answers3

5

Change your Integer.parseInt(quantityString); to Integer.parseInt(quantityString.trim()); and your code will work fine!

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
  • I have changed that code and the error is gone! However, theres no display when I click the Add to Order button. – Warren Xie Apr 17 '14 at 04:43
3

You have an additional space in the end. Trim it to remove that spcace.

quantityInteger = Integer.parseInt(quantityString.trim());
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • I have changed that code and the error is gone! However, theres no display when I click the Add to Order button – Warren Xie Apr 17 '14 at 05:45
0

try this i found an empty space in the console message "Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1 "(after one there is a space) so try to find if there is any empty space taken into account by mistake, so parserint is trying to parse string + empty space.

optimus
  • 729
  • 2
  • 12
  • 36