0

How to control character input in java dialog boxes?

For example my variable was string and I just want the users can only input numbers? This was my code tempStr = JOptionPane.ShowInputDialog("Age: ", JOptionPane.QUESTION_MESSAGE);

18th Feb 2020, 5:07 PM
Jason Cuerbo
Jason Cuerbo - avatar
5 Réponses
+ 2
a) the user can enter anything and you check if the string contains only numbers. If yes, continue with the program. If not, show a error message box and ask for age again. b) You could add a keylistener to your showInputDialog(). I think it should be possible to prevent the user entering nondigits using the keycode of the keyevent. I think the best is you describe exactly how you imagine it and I try to help you with the code.
18th Feb 2020, 8:10 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Jason Cuerbo Here are two sample codes: https://code.sololearn.com/cOVMncfFRe8L/?ref=app https://code.sololearn.com/cYOCCt8GEn2h/?ref=app Hope it helps. If you have questions just ask :)
19th Feb 2020, 6:01 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thank you so much, Denise.
23rd Feb 2020, 4:43 PM
Jason Cuerbo
Jason Cuerbo - avatar
+ 1
Jason Cuerbo Your welcome :)
23rd Feb 2020, 4:55 PM
Denise Roßberg
Denise Roßberg - avatar
0
Thank you so much. I will describe it the best I can. So our teacher give us a task to make a program about enrollment. I was only having a hard time in the age part At first tempStr = JOptionPane.ShowInputDialog("Age: ", JOptionPane.QUESTION_MESSAGE); age = integer.parseInt(tempStr); But when i input any character that is not integer it gives an error. I want that the users can only input >=15 and <=85 numbers. Our teacher said that research about keypressed or control character input but I couldn't find any. I think that my teacher was saying about B.
19th Feb 2020, 3:16 AM
Jason Cuerbo
Jason Cuerbo - avatar