0

Java _printing error!

hello guys! I trying to write a chunk of code that will take numbers from 1 to 6 or 'q' character as an input. now my question is how can I limit the answer and print error if the user enters his\her name or a piece of number like 25g? I appreciate your attention.

16th Apr 2018, 3:08 PM
Linoos
3 Answers
+ 1
I'm so thankful guys, I'm a new learner in java so I was not familiar with Exception syntax, I will try to learn it. TANX
16th Apr 2018, 3:38 PM
Linoos
0
Check each character in your input string and if its something else, throw an exception or demand another input.
16th Apr 2018, 3:21 PM
Jan Ơtěch
Jan Ơtěch - avatar
0
This code snippet only accepts ints (whole numbers) and obviously put this in the main or some other method Scanner usrInput = new Scanner(System.in); int number = 0; try { number = usrInput.nextInt(); } catch(Exception ValueError) { System.out.println("Incorrect input"); }
16th Apr 2018, 3:28 PM
TurtleShell
TurtleShell - avatar