• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

catch statements

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ive been having trouble implementing a catch statement into this method. I want to catch all values but y and n.

Any suggestions?



Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
catch block
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot chad, ill ake your advice
reply
    Bookmark Topic Watch Topic
  • New Topic