How to make the java code restart if user enter false number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to make the java code restart if user enter false number?

18th Aug 2017, 2:32 PM
Manal Medhat
Manal Medhat - avatar
3 Answers
+ 1
You can try something like that (but it will not work in SL Playground): * * * Scanner scan = new Scanner(System.in); int n = 0; while (true) { try { System.out.print("Enter a number [1..1000]: "); n = Integer.parseInt(scan.nextLine()); // Break if 'n' is in range 1..1000 if (n > 0 && n < 1001) { break; } } catch (Exception e) { System.out.println("(!) Wrong input...\n"); } } System.out.println("The number is: " + n); * * *
18th Aug 2017, 3:06 PM
Boris Batinkov
Boris Batinkov - avatar
+ 3
You could: * Use a loop that continues if the input is false or * re-call the method if the input is false
18th Aug 2017, 3:07 PM
Rrestoring faith
Rrestoring faith - avatar
0
do you have an example? As java restarts after your program has finished with an output.
18th Aug 2017, 2:38 PM
D_Stark
D_Stark - avatar