aspose file tools*
The moose likes Beginning Java and the fly likes Error Checking - Can I bypass certain sections of code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error Checking - Can I bypass certain sections of code?" Watch "Error Checking - Can I bypass certain sections of code?" New topic
Author

Error Checking - Can I bypass certain sections of code?

Ryan Wamsat
Greenhorn

Joined: Aug 06, 2012
Posts: 13
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

Joined: Mar 17, 2011
Posts: 7652
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ryan Wamsat
Greenhorn

Joined: Aug 06, 2012
Posts: 13
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

Joined: Mar 17, 2011
Posts: 7652
    
  19

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

Joined: May 30, 2012
Posts: 679
Just out of interest - why didn't you use an else like you have done with all your other tests ?
Ryan Wamsat
Greenhorn

Joined: Aug 06, 2012
Posts: 13
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

Joined: Aug 06, 2012
Posts: 13
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

Joined: May 30, 2012
Posts: 679
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
 
 
subject: Error Checking - Can I bypass certain sections of code?