I have a segment of code with a try/catch within a loop that isn't acting right by my knowledge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a segment of code with a try/catch within a loop that isn't acting right by my knowledge

The code is supposed to allow the exception to print and have the user type in a new variable without exiting the while loop. ... while(wrong){ try{guess=s.nextInt();} catch(Exception e){ System.out.println("You didnt type a number!"); break;} ... This runs just fine but if the user is already invested into a game, say 10 guesses, then all of their progress is lost and the code is redirected out of the current while loop. Its a terrible bug.

15th Oct 2018, 12:53 AM
Evan Martine
1 Answer
0
Hi. You'd better mention the part of code where statistics is changing to make it easier for troubleshooting. But if you don't want to get out of the loop then try to use "continue;" instead of "break;" statement Or you can try to declare guess count variable outside the block, where you face such problem. I think it shouldn't be lost in case of failure in while loop
15th Oct 2018, 8:57 PM
microBIG
microBIG - avatar