I have to validate my user input for an integer value and prompt the user to reenter if an invalid value is entered. Thus far I have put together this method below. i believe the logic is correct but I need to get the user to reenter input again after the error message.
void validateItemquantity() {
boolean error = true;
while (error) {
try{
Integer.parseInt(itemQuantityinput.getText());
error = false;
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Item quantity must be a positive number, please reenter");
error = true;
}
}
}
Part of code from jframe method:
private void bnPurchaseActionPerformed(java.awt.event.ActionEvent evt) {
String itemCode;
int itemQuantity, itemPrice, itemCost, totalCost ;
validateItemquantity();
itemCode = itemCodeinput.getText();
itemQuantity = Integer.parseInt(itemQuantityinput.getText());
itemPrice = catalog.searchCatalog(itemCode);
itemCost = payment.calculateItemcost(itemQuantity,itemPrice);
totalCost = payment.calculateTotalcost(itemCost);
I think the loop should be in the calling method.
And the calling part should be something like that