Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exit this Do-While w/out an int?

 
Brian Pianczk
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am trying to figure out a way to exit this do-while. I am wondering how it can be done w/out using a number.
I originally did it with a while num > 0 and used an int num variable, but a neg number can be even or odd.
My new approach gives me an error (unknown source) when it reads a blank entry.





Error when I just use a space or enter.
c:\Program Files\Java>java Test
Enter an integer:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""

at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Test.getNum(Test.java:17)
at Test.main(Test.java:7)

c:\Program Files\Java>
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why dont you add the same check num.isEmpty() inside the loop and return; the method.

Also check if the String is number otherwise you get numberFormat Exception
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,
How about using a boolean?
 
amitabh mehra
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
or use a try-catch block:
 
Brian Pianczk
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitabh mehra wrote:or use a try-catch block:


Thanks for the quick responses!

I like this idea, but I am guessing it would be bad programming to use an exception to exit a program?

Like if I were to say "Enter an integer, or just return to exit: " then use the exception to exit? and display "goodbye" in the exception.


Edit: never mind this idea, doesn't solve the issue if someone enters a non integer.


 
Brian Pianczk
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Brian,
How about using a boolean?


Still thinking about how I can apply this. Are you saying make the whole method a boolean?
 
amitabh mehra
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Pianczk wrote:
amitabh mehra wrote:or use a try-catch block:


Thanks for the quick responses!

I like this idea, but I am guessing it would be bad programming to use an exception to exit a program?

Like if I were to say "Enter an integer, or just return to exit: " then use the exception to exit? and display "goodbye" in the exception.




the try-catch I suggested was never meant as an "exit" route. It was for that "dirty" exception stack trace which you would not want the user to see.
For exit purpose, perhaps you can prompt the user to enter something like 'quit' and change your code a bit in the way like:


Now this will still give "Not a number" message to user in case non-number is entered and allow the user to continue. else 'quit' the application.
 
Brian Pianczk
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
amitabh mehra wrote:
the try-catch I suggested was never meant as an "exit" route. It was for that "dirty" exception stack trace which you would not want the user to see.
For exit purpose, perhaps you can prompt the user to enter something like 'quit' and change your code a bit in the way like:


Now this will still give "Not a number" message to user in case non-number is entered and allow the user to continue. else 'quit' the application.


Thanks, I am trying to learn java, and all these suggestions help. It is amazing how many solutions there are to one problem, and each one I learn something new.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic