• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error Checking - Can I bypass certain sections of code?

 
Ryan Wamsat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please forgive me if this is overly basic, I'm trying to teach myself to program in Java and I've recently started. To that end, I've written the following simple number guessing game.
At this point, I'm trying to figure out how to error check the user's input. For example, if the chosen game is Easy, the values are between 1 and 10. If the user guess is 11 or above, I want to print an error stating the fact and deduct a try.
From there it should give the user an opportunity to guess again without running that poorly chosen guess through the game (and thereby deducting yet another point).

I'm aware that I could put this Error Checking section within the Game Input Evaluation section.... but, I'm afraid that by making too many nested if conditions, my code will become unreadable.

Any help or suggestions would be appreciated.


 
Winston Gutkowski
Bartender
Pie
Posts: 10272
60
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Wamsat wrote:From there it should give the user an opportunity to guess again without running that poorly chosen guess through the game (and thereby deducting yet another point)...

I think what you're looking for is continue. It's another form of loop control, like break (which drops out of a loop immediately), except that it says "skip the rest of the code in this loop and continue with the next iteration". However, there is another (and probably better) alternative: put your "error checking" and "game input evaluation" sections into methods.

Winston
 
Ryan Wamsat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:I think what you're looking for is continue. It's another form of loop control, like break (which drops out of a loop immediately), except that it says "skip the rest of the code in this loop and continue with the next iteration". However, there is another (and probably better) alternative: put your "error checking" and "game input evaluation" sections into methods.



Thank you. The continue function was exactly what I was looking for. I will try putting these into methods (as suggested) in the next version of this game.
I appreciate your taking the time to answer such basic questions.

Ryan
 
Winston Gutkowski
Bartender
Pie
Posts: 10272
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Wamsat wrote:I appreciate your taking the time to answer such basic questions.

No probs. We were all where you are once, and questions are only basic if you know the answer.

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of interest - why didn't you use an else like you have done with all your other tests ?
 
Ryan Wamsat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart A. Burkett wrote:Just out of interest - why didn't you use an else like you have done with all your other tests ?


I didn't see the need. I only needed to error check the two conditions: higher than or less than defined parameters.
So, I used an if statement with the or pipes to check whether either condition is true. Plus, I wanted to see how that worked.

If you have an alternative suggestion, I'm willing to learn.

Thanks,
Ryan
 
Ryan Wamsat
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:However, there is another (and probably better) alternative: put your "error checking" and "game input evaluation" sections into methods.


Ok, I've been racking my brain on how to go about doing this. And, I've come up with nothing.
It would seem that I have to:
1. Call the Game() method.
2. Get the user's input.
3. Call the Error_Checking() method (and therefore exit the Game() method)
4. Assuming the guess was valid, I would have to re-enter the Game() method in the area where I exited before.

I'm sure there's a way to go about this, but I haven't a clue where to begin. Suggestions or links to a similar concept would be greatly appreciated.

 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Wamsat wrote:I didn't see the need.

Well, it would have fixed the problem you were having.

Ryan Wamsat wrote:If the user guess is 11 or above, I want to print an error stating the fact and deduct a try.
From there it should give the user an opportunity to guess again without running that poorly chosen guess through the game (and thereby deducting yet another point).


Note the else I've added on line 6
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic