Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
TDD for a Shopping Website LiveProject
this week in the
Testing
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Beginning Java
How to reiterate a do while loop after an exception is caught, run time error handling issue
fayzal fauzie
Greenhorn
Posts: 4
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
HI Guys,
This programs runs good should no exception is caught, and am having difficulty figuring how to handle the exception part
Basicly i just need to rerun the loop when the exception is caught.
Any input is greatly appreciated.
Thanks in advance.
public static void main(String[] args) { // TODO Auto-generated method stub int hourin, maxHour=23; boolean bool = true; Scanner sc2 = new Scanner(System.in); // Requires J2SE 1.5 do { try{ System.out.println("Enter hour ? : 00 - 23 " ); hourin = sc2.nextInt(); if( hourin > maxHour ){ System.out.println("Hour value not valid: 00 -23"); System.out.println("Please enter again"); }else bool = false; }catch (NumberFormatException ex) { //this is caught when number in decimal is entered System.err.println("Not a valid number: " + ex); return; } catch(InputMismatchException ex){ //this is caught when non integer is entered System.err.println("Not a valid number: " + ex); return; } } while(bool); } }
Rob Spoor
Sheriff
Posts: 22647
126
I like...
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Just don't return?
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to loop back after an exception is caught?
Largest/Greatest of n numbers
Error incrementing value in catch block
Feedback.
NumberFormatException thrown from parsing string to int
More...