Jump to content

como restringir o imput a int


carvalho2707

Recommended Posts

Para fazer isso bem da forma melhor tens de usar try Catch... Não deves estar familiarizado mas chama-se de tratamento de excepções. Quando existe uma excepção o java manda um erro quer pode ser apanhado e tratado.

Neste caso fazes o try(tentar) concerter para inteiro se der alguma excepção (como a excepção de converter um caracter num numero) e apanhas essa excepção com o catch, no casso a excepção que o java manda quando tenta converter uma string para numero é NumberFormatException por isso a maneira de a tratar é esta:

try{
int teste = Integer.parseInt( JOptionPane.showInputDialog("Inserir numero"));
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(null, "O numero é inválido!");
}

Poder aprender mais aquí: http://www.exampledepot.com/egs/Java%20Language/TryCatch.html

Edited by skinie18
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.