• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with incorrect end after error handling  RSS feed

 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am writing a guessing game (just to learn java, no other purpose )
It create's a random number that must be guessed by the user.
Then it asks for user input.
If the number is in the correct range but not the exact number it says if the randomnumber is bigger or smaller.
If the number is correct they get a congratulation message.

The problem is in the error handling. I used a (NumberFormatException nFE) on the parseint to make sure its a real integer.
This works. The problem is that when there is a mistake made the program gives the congratulations message and afterwards sais the number is bigger and asks for a new guess. (entering correct number gives congratulation message and closes the program).

Any idea where my mistake is?


 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few suggestions:
1. Don't recursively call the vraagEnTestNummer() method. That is, don't call vraagEnTestNummer() from within vraagEnTestNummer().
2. You catch NumberFormatException but then call vraagEnTestNummer(). So after it returns from vraagEnTestNummer(), it will continue processing as if the user guessed the number 0. Change the NumberFormatException code to inform the user of the mistake and prompt for input again (See suggestion 3)
3. Rather than recursively calling vraagEnTestNummer(), use a while loop. The first thing in the while loop prompts the user for a number. The while loop terminates when the user has correctly guessed the number.
 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i am going to try it with a while loop now. (stupid to not think of that in the first place :s )
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are doing it recursively. Once you reach line 29 and call your method again, it will execute till its end and then continue execution of previously executed method from line 29 onward. In case of a wrong number input you need to notify the user of the wrong input and then end up on line 46.

You might want to consider doing the whole program in a loop rather than recursively.
 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i changed the Verwerking Class to this:



Seems to work perfectly now. Thanks all.
 
Suhrid Karthik
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the logic is better implemented in a while loop instead of recursion. However, in the recursive method itself, you could terminate the program by calling System.exit() when the user guesses the correct number instead of allowing the method to return.
 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suhrid Karthik wrote:Yes the logic is better implemented in a while loop instead of recursion. However, in the recursive method itself, you could terminate the program by calling System.exit() when the user guesses the correct number instead of allowing the method to return.


Also true. It would solve the problem i had.
I like the while loop better because the code looks somewhat clearer to me. I am wandering why I never thought of it in the first place.
Is there a "common practice" or a "best way" to do this?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.exit()?? A dangerous method to use.
I agree that a loop fits number entry better than recursion, however.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like your creating a new Reader for every iteration of the loop, however.
Since you are probably using Java6, have you tried the Scanner class.
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for being a nitpicker Jarco, but it's "het nummer", not "de nummer" ;P
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just a question on the last bit of code Jarco posted(I hope this isn't considered Hijacking!)Does that code snippet work?
is it normal to have 3 "if statements" in a row and then 3"else statements" in a row? I am new so maybe this is something i have just never seen ??

or does that maybe only work with the "Dutch" JVM ;)

Thanks
Mike
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, this is perfectly legal, and it is used a lot. Nested if statements are very common.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan never seen a loop made like that but good to know.

Mike
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't confuse if statements with loops! They're not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!