File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes catch statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "catch statements" Watch "catch statements" New topic
Author

catch statements

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Ive been having trouble implementing a catch statement into this method. I want to catch all values but y and n.

Any suggestions?



Thanks


Stephen Foy - Microsoft Application Development Consultant
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I'm a little confused about your question. Are you talking about creating a catch block to handle an exception? Or are you talking about checking values that the user enters with if statements like you have?
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
catch block
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
Errr... a catch block is half of a try/catch block, which as Keith already pointed out, is an error/exception catching block. For instance:


Are you maybe trying to do something like this?

That seems maybe backwards from what it appears that you are trying to do. Could you maybe help us out and explain what you are trying to do?

EDIT: Or are you saying that you want to see if it is a yes or no answer first, and if it isn't, then skip the code altogether?
[ December 13, 2005: Message edited by: C Clites ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

ste, does this mean you're considering throwing an exception if the user enters something other than 'n' or 'y'?
[ December 13, 2005: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Yea, if the user enter y, or n, then the program will do something, but anything else throws an exception. I just cant seem to get the catch to work.
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I'm not convinced that you want to catch anything, and I'm not sure how to explain to you that I don't think that is what you want to do. A try/catch statement is for catching program errors..

Strictly defined
Definition: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program.


What I think you really want to do is something like as follows:

Some ways of handling bad input are ignoring the input (bad idea), or somehow letting the user know that their input was bad (better). I would consider that a normal event, and not one that disrupts the flow of a program.

Unless you are really interested in generating custom exceptions..
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Thanks, i did do that initially, but the problem is when i put something bad in, it generates the game numbers again. Ill figure it out. Try this and see what i mean. The uses is only allowed 1 roll each turn.

Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I understand what you are trying to do, and after looking at your code a bit, I'm pretty sure you know what you need to do, although I think some restructuring is called for. I suggest maybe sitting down with your pencil and paper and thinking it through a bit... you have so many loops and whiles that it is hard to keep track of everything.

If I am following your descriptions, basically you have three rolls to get the highest score. On each of the three rolls, you have the option of saving one of the scores. After three rolls, the scores are added, and the score printed.

One place I would look at starting is

Keep the totals and amount of rolls outside of the loop.. that way they don't get reset each time.

but the problem is when i put something bad in, it generates the game numbers again. Ill figure it out. Try this and see what i mean.


What I would do in this case is that if the user puts in something bad (something other than y or n), then make it default to no (do not save the highest roll). That way, you only need to check if the user chose yes.. so you would have something like:

Or if you want, you can check if it is a y or n, and if it isn't, prompt the user for a valid input.

Another thing that may be nit-picky... you don't need an array to hold the highest values. Since the values are going to be totaled, why not just keep a single int to keep track of the running score? Then there is no need for a seperate loop to total the scores later. You only need to print out the total.

Also, you don't need an array to hold the rolls either, you just need a single int to hold the highest roll for that round. It is initially a little more tricky to code, but your code will be more simple, and easier to read.

I think the one thing that will help you the most is to use a pencil and paper and draw out your program. It should give you a much clearer idea of what you want to do...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Chad Clites:
... What I would do in this case is that if the user puts in something bad (something other than y or n), then make it default to no...

Good advice.
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Thanks alot chad, ill ake your advice
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: catch statements