• Post Reply Bookmark Topic Watch Topic
  • New Topic

Do while loop-without using try catch  RSS feed

 
Miro Zuber
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am wondering how could I possibly make this running without using try/catch.
The loop should terminates when the input is of a type integer.Otherwise it should continue-prompting the user for an entry until integer is typed in.
Not sure what could be used in the code-parseInt-probably?
 
Marc Cracco
Ranch Hand
Posts: 80
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I didn't get to test this but it should work...

you should take a look at apache commons... The one I used is part of the commons-lang package. Documentation here.
 
Miro Zuber
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.It looks quite useful!
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are far better ways than that.
  • 1: You don't want that sort of thing in your class in the first place. You want a utility class possibly called KeyboardInputs.
  • 2: You can always avoid the Exception from Scanner like this.
  • 3: You can use the simpler version of what Rob Spoor showed me, because you never close a Scanner pointing to System.in.

  •  
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And welcome to the Ranch
     
    Marc Cracco
    Ranch Hand
    Posts: 80
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
  • 1: You don't want that sort of thing in your class in the first place. You want a utility class possibly called KeyboardInputs.



  • What part are you referring to?
     
    Campbell Ritchie
    Marshal
    Posts: 56570
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Keyboard input. Put it in a utility class, otherwise your method will look like this:-
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!