File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HiLo game and NumberFormatException

 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking about a method like



What do you think about that?
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja, thanks again

what will you return when fail occurs?
at a first glance looks like my scenario (return -1)
 
Timmy Marks
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, Timmy
now i'v got it

looks great
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's the refactored version:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic