Warren Xie
Warren Xie

Reputation: 21

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1 "

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

Upvotes: 2

Views: 20891

Answers (3)

optimus
optimus

Reputation: 729

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.

Upvotes: 0

Sazzadur Rahaman
Sazzadur Rahaman

Reputation: 7126

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

Upvotes: 5

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32488

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

quantityInteger = Integer.parseInt(quantityString.trim());

Upvotes: 3

Related Questions