why is it keep outputing "Please enter YOUR AGE again !"? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why is it keep outputing "Please enter YOUR AGE again !"?

System.out.println("Please enter your age."); int x = 1; do{ try{ int age = info.nextInt(); if (age < 0 || age >130){ System.out.println(""); System.out.println("Please enter YOUR AGE again."); }else{ x = 2; } }catch(Exception e){ System.out.println(""); System.out.println("Please enter YOUR AGE again !"); } }while(x == 1); my original purpose is get others' ages. i am wondering why i type "hfbasjbjsbk",it will keep outputing "Please enter YOUR AGE again !" shouldn't it ask me to enter a age again?

21st Oct 2017, 4:42 PM
YuHai
YuHai - avatar
10 Answers
+ 1
What are you hoping it will print out? But the main reason is because it isn't an integer, it will go to the catch which will print it out again. Long story short, it can't translate a string to an integer. Hope this helps.
21st Oct 2017, 9:38 PM
Ryan Wakefield
Ryan Wakefield - avatar
+ 1
sorry,Gordie and Ryan Wakefield. i dont expect this kind of thing to happen.please accept my apology
22nd Oct 2017, 6:18 AM
YuHai
YuHai - avatar
+ 1
yeah, I have that autocorrect with voice to text running. guess I really should have read that statement over again. lol And I agree, the code in general does need rework as the logic is not sound. I would suggest going back and doing sudo code. this is always a good place to go when having issues.
22nd Oct 2017, 1:30 PM
Ryan Wakefield
Ryan Wakefield - avatar
+ 1
thank you,@Ryan and@Gordie.let me rearrange my code again.thanks for giving me an important lesson and practial advice.
22nd Oct 2017, 3:23 PM
YuHai
YuHai - avatar
+ 1
you are more than welcome. as I said, don't hesitate to go back to sudo code. it might help more than you realize to do the logic cleaner. :)
22nd Oct 2017, 4:21 PM
Ryan Wakefield
Ryan Wakefield - avatar
0
let me explain again, first ,i enter a string ,and it will go to catch. when it has run through the code of catch,shouldnt it go back to do,try,and then ask me to enter another value of age?
22nd Oct 2017, 12:47 AM
YuHai
YuHai - avatar
0
So just to verify, after you enter the string, does the program exit or does it infinitely loop asking for the age?
22nd Oct 2017, 2:28 AM
Ryan Wakefield
Ryan Wakefield - avatar
0
so,Jordie,can you explain why it will loop
22nd Oct 2017, 3:16 AM
YuHai
YuHai - avatar
0
if you remove out of the catch block, then it won't get the try the first time through. Let me think on this as to a suggestion on rework.
22nd Oct 2017, 1:14 PM
Ryan Wakefield
Ryan Wakefield - avatar
0
sorry, meant thd try block. if you remove it out of the try block. so in other words, it will print out the request for an age, then it will hit the tribe Loch and never try to read in the line and jump to the catch and then print out the request again and then get the inputted information. Basically, you are doubling up on the first request for an age. This will only happen when you first run the program.
22nd Oct 2017, 1:22 PM
Ryan Wakefield
Ryan Wakefield - avatar