Java – use jtextfield – cannot convert value to int
•
Java
I want to start from JCombo@R_333_2419 @Get the selected value in, search for it in the database, and update the quantity in the database with the value in jtextfield This is the code:
Object selected = jCombo@R_333_2419@1.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 received 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 enter the value "1" in the text field
Solution
If there is a space in the string entered into parseint(), use trim()
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
