I have a separate class for Items and an Inventory_MP class for the GUI. My task here is to remove the Item typed by the user and let it show in the interface that it has been removed. For some reason, I can't remove it. The error says:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 2" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:481) at java.lang.Integer.parseInt(Integer.java:527)
if(e.getSource() == remButton){
String delitem = nameField.getText();
String delnum = numField.getText();
String delqty = qtyField.getText();
String delprice = priceField.getText();
Items v = new Items(delitem, delnum, Integer.parseInt(delqty), Double.parseDouble(delprice));
removeItems(delitem, delnum, delqty,delprice);
public void removeItems(String delitem, String delnum, String delqty, String delprice){
Items v = new Items();
itemModel.removeElementAt(itemCollection.indexOf(delitem)); //removes element in the JList
itemnumModel.removeElementAt(itemCollection.indexOf(delnum));
qtyModel.removeElementAt(itemCollection.indexOf(delqty));
priceModel.removeElementAt(itemCollection.indexOf(delprice));
itemCollection.remove(searchItems(delitem)); //removes element in the arrayList
itemText.setText(" ");
numText.setText(" ");
qtyText.setText(" ");
priceText.setText(" ");
window.validate();
window.repaint();
}
You're error is occurring on the Integer.parseInt call, it is because of the leading space at the front of your input string " 2".
String delqty = qtyField.getText();
qtyField.getText();
is returning the string with a leading space. If you remove the space in qtyField it will fix this error, there may still be other errors in your code, but the call to Integer.parseInt will work fine.
As said by ekcrisp, the error is due to delqty
which contains a ' '
, and so can't be parse into int
.
You can do some pretreatment on delqty
. For example delqty = delqty.replace(' ', '')
should do the trick in your case, however I suppose qtyField
is a JTextField
so the user can write anything. You will need a lots of pretreatment on delqty
, or a smart user =)
Another solution should be to use the good Swing component, a JSlider
or a JSpinner
may be more appropriate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.