aspose file tools*
The moose likes Beginning Java and the fly likes Exit this Do-While w/out an int? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exit this Do-While w/out an int?" Watch "Exit this Do-While w/out an int?" New topic
Author

Exit this Do-While w/out an int?

Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
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

Joined: Jan 01, 2009
Posts: 874


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

Joined: May 26, 2003
Posts: 31079
    
163

Brian,
How about using a boolean?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
amitabh mehra
Ranch Hand

Joined: Dec 05, 2006
Posts: 98
or use a try-catch block:
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
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

Joined: Jan 26, 2009
Posts: 45
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

Joined: Dec 05, 2006
Posts: 98
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

Joined: Jan 26, 2009
Posts: 45
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exit this Do-While w/out an int?