wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes HiLo game and NumberFormatException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HiLo game and NumberFormatException" Watch "HiLo game and NumberFormatException" New topic
Author

HiLo game and NumberFormatException

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i'm crafting this game; all went well till the point where things can go wrong: user's input is not an int...

where/how should i manage it?

i tried a try/catch block, but since my method returns an int i'm forced to return some int... so i returned -1, but wont work either

here's my code:

any ideas would be wellcome

TiA
[ June 12, 2005: Message edited by: miguel lisboa ]

java amateur
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
somehow i sorted it out, but i'm not happy with its elegance

how can i improve it?

TiA
[ June 12, 2005: Message edited by: miguel lisboa ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Instead of the converte method, I'd probably have a method that reads from System.in until the user enters the number, and then returns it as an int.

How does that sound to you? (If you try to implement this, remember that you can have a return statement inside a loop.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks, Ilja

me and IO is like entering this airplane's cockpit: hundreds of commands, pilot clearly explains and uses some of them, plane does liftoff, flies and lands, but if someone asks me to do it, i for sure wouldnt be able to!

well, i tried this bit of code:

but it just prints the first input (from 17 just prints 1)

all this to tell you i dont know from where to begin with your suggested method...

can you pls give me any hint?

TiA
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Sorry, I wasn't clear:

I think the way you read input from the user and try to parse it was already very well. What I wanted to address was your error handling in the case that a non-number was entered.

Currently, your processing looks like this:

loop
- read user input
- if input legal
-- process input

What I tried to suggest is to change that to using two loops:

loop
- loop until legal input
-- read user input
- process input

That way, you don't need to return any number until the user actually entered one - no -1 return value needed. (Assuming that the inner loop is encapsulated into its own method.)

Does that help?
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i tried nested loops but ended with infinite ones
instead i used another method that returns a boolean instead:do you agree?

BTW how was it with your version?

thanks again

EDITED:
still, i notice some duplication within my methods converte() and verifica()
[ June 12, 2005: Message edited by: miguel lisboa ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I was thinking about a method like



What do you think about that?
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Ilja, thanks again

what will you return when fail occurs?
at a first glance looks like my scenario (return -1)
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
The point of Ilja's pseudocode above is that upon catching a NumberFormatException, it stays in the while(true) loop, so the user would have to enter another line (hopefully with a valid number). Each time the user enters a line, try to parse it, which either returns an int or throws a NFE. In the catch block, you can warn the user or count the number of wrong attempts (initialize before while loop) so that you can abort after x tries, or whatever. It doesn't return anything on an improper input.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks, Timmy
now i'v got it

looks great
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
here's the refactored version:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HiLo game and NumberFormatException