I'm doing a Java code using JOptionPane, and in it, I created an InputDialog that returns the value "1", "2" or "3" entered by the user to a String variable called "return". That done, I need to convert it to int, using Integer.parseInt()
. However, if the user types something other than number would give an error in execution at the time of converting to integer , and I do not want this.
String retorno;
int op;
retorno = JOptionPane.showInputDialog(null, " 1- Criar uma conta \n 2- Acessar uma conta \n 3- Sair");
op = Integer.parseInt(retorno); //se não for número, da erro =/
How to construct the code for when the user enters a value, make sure it is number, and if not, to return an error to the user instead of sticking and stop execution ?