So my program asks the user to input two integers then if the user inputs a string it will display an error message "Invalid! Please input an integer". But after the error message shows up it displays an error.
This is the error but I managed to convert the JTextField to String then to Integer
Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at SimpleCalculator1.main(SimpleCalculator1.java:52)
And this is my code
import javax.swing.*;
public class SimpleCalculator1
{
public static void main(String [] args)
{
int error1, error2;
JTextField field1 = new JTextField();
JTextField field2 = new JTextField();
JTextField field3 = new JTextField();
Object[] message =
{
"Enter first integer:", field1,
"Input second integer:", field2,
"Choose the operation to be used\nAddition(+)\nSubtraction(-)\nMultiplication(*)\nDivision(/):", field3,
};
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
boolean shouldExit = false;
while(!shouldexit)
{
if(option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
switch(operation = field3.getText())
{
case "+":
case "Addition":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
}
case "-":
case "Subtraction":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
}
case "*":
case "Multiplication":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
}
case "/":
case "Division":
{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
}
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
else if (option == JOptionPane.CANCEL_OPTION||)
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
shouldExit = true;
}
}
}
Even though you catch NumberFormatException
for field 1 and display an error, you continue on after the catch and attempt to parse a number from field3,which is not likely to be entered yet and then you again attempt to parse field 1 and 2 as numeric values. Extend your catch to wrap the process of all 3 fields and force the user to re-enter a valid value.
Moving your catch block to include parsing of all textfields and forcing the user to input a correct value can be done by modifying your while loop as shown here:
boolean exit = false;
while(!exit) {
int option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
if(option == JOptionPane.OK_OPTION)
{
String value1 = field1.getText();
String value2 = field2.getText();
String operation = field3.getText();
try{
int num1 = Integer.parseInt(value1);
int num2 = Integer.parseInt(value2);
operation = field3.getText();
if(operation != null){
operation = operation.toLowerCase();
}
switch(operation) {
case "+":
case "addition":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1+num2;
JOptionPane.showMessageDialog(null,"The sum is: "+result);
break;
case "-":
case "subtraction":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1-num2;
JOptionPane.showMessageDialog(null,"The difference is: "+result);
break;
case "*":
case "multiplication":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
int result = num1*num2;
JOptionPane.showMessageDialog(null,"The product is: "+result);
break;
case "/":
case "division":
num1 = Integer.parseInt(value1);
num2 = Integer.parseInt(value2);
double result = (double)num1/num2;
JOptionPane.showMessageDialog(null,"The quotient is: "+result);
break;
default:
error1 = JOptionPane.showConfirmDialog(null,"Invalid operation, please try again","Error",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE); //I need help here for creating a loop that will take me back to the beginning.
}
//option = JOptionPane.showConfirmDialog(null, message, "SimpleCalculator", JOptionPane.OK_CANCEL_OPTION);
}
catch(NumberFormatException ne)
{
JOptionPane.showConfirmDialog(null,"Invalid! Please input an integer","Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE); //When I press ok, I need help to bring me back to input entries
}
}
else if (option == JOptionPane.CANCEL_OPTION)
{
JOptionPane.showMessageDialog(null,"Thank you for using our program!");
exit = true;
}
}