Regarding For Simple Menus Using Do-while Loops In Java - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Regarding For Simple Menus Using Do-while Loops In Java (334 Views)
1 Reply
| Regarding For Simple Menus Using Do-while Loops In Java by Arjunkumar09(op): 6:53am On Feb 15, 2022 |
Hello All, I am trying to make a simple menu using a do-while loop in java. I have used this source to run the code and its looks like this: int choice; The problem is, every time I try to run it, it throws "java.util.NoSuchElementException". The full error is below: Exception in thread "main" java.util.NoSuchElementException I know that this is because scanChoice.hasNextInt() returns false, but I'm not sure how to fix this. When I add an if statement (if (scanChoice.hasNextInt())), the method scanChoice.hasNextInt() still returns false, so it just passes over the line that initializes the variable choice, and that variable never gets initialized. Anyone know how to fix this? Any suggestions? |
| Re: Regarding For Simple Menus Using Do-while Loops In Java by airsaylongcome: 10:24am On Feb 15, 2022 |
Try this while ((choice < 1) || (choice > 3)); |
| Re: Regarding For Simple Menus Using Do-while Loops In Java by islamics(m): 2:14pm On Feb 15, 2022 |
Arjunkumar09:Your snippet run normal in my system ooo. Perhaps you should run it a local machine and see. I tried it using same online compiler too but that site was too clumsy for me. Then I tried with other online compiler (programiz and ideone) and it works. My thought is that, standard input maybe disabled for the online compiler. Hope the Scanner class was imported in your code? |
What Is Loop? The Concept Of Loops In Programming| Coding For Kids | 5-9 • Tutorials On Having Deep Understanding About Loops In JS And How To Iterate • Regarding For DBMS Query! • 2 • 3 • 4
Soft Skills For Software Developers By Morgan J Lopes • BackEnd Engineer For Complex Projects • List Of Websites To Find Software Developer Jobs
;