3
I’m making a code in Java using Joptionpane, and in it, I created an Inputdialog that returns the value "1","2" or "3" typed by the user to a String variable called "return". Once this is done, I need to convert it to int, using the Integer.parseInt()
. However, if the user type something other than number would give error in running when converting to integer, and I don’t want it.
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 build code for when user enters a value, make sure it is number, and if not, return an error to the user instead of giving up and stop the execution?
You only want to accept the values 1, 2 or 3?
– Jéf Bueno
@jbueno yes! Only these values.
– Felipe
http://www.guj.com.br/java/66577-como-verifiar-se-a-string-e-letra-ou-numero
– DiegoAugusto
You can only choose one answer like the "correct" ;-) PS: Feel free to choose the one you found the most suitable for your situation.
– Math