Using JTextField - Unable to convert value to int

I want to get the selected value from JComboBox, search the database and update the number in the database with the value JTextField. Here is the code:

    Object selected = jComboBox1.getSelectedItem();
    String album = (String)selected;
    int qty=Integer.parseInt(jTextField7.getText());
    String query2="update  productlist set QtyAvail=? " +
                "where Album=?";
        try
        {
            PreparedStatement ps2=con.prepareStatement(query2);
            ps2.setInt(1, qty);
            ps2.setString(2,album);
            int res1=ps2.executeUpdate();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

I get this error:

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)
    at AddProductPanel.jButton2ActionPerformed(AddProductPanel.java:341)
    at AddProductPanel.access$4(AddProductPanel.java:335)
    at AddProductPanel$5.actionPerformed(AddProductPanel.java:133)

I entered the value "1" in the text box.

+3
source share
4 answers

There is a space in the input line parseInt(), usetrim()

+5
source

I want to get the selected value from JComboBox, search the database and update the number in the database with the value JTextField.

+4

Your line contains a space.

Use the option before processing:

int qty=Integer.parseInt(jTextField7.getText().trim());
+2
source

@Nivedita Gautam: It seems that before 1added space a blank. Try the following statement:

int qty = Integer.parseInt(jTextField7.getText().trim());
+1
source

All Articles