Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-25-2008, 12:03 AM   PM User | #1
BWiz
Regular Coder

 
BWiz's Avatar
 
Join Date: Mar 2006
Location: Sol System
Posts: 471
Thanks: 7
Thanked 21 Times in 21 Posts
BWiz is an unknown quantity at this point
Stopping Java Application depending on user input

I'm coding a little program that calculates the difference between an arrival time and departure time. Below is a little snippet of the app that calculates the difference:

Code:
public int DifferenceInHours(int Departure, int Arrival) {
        int difference = 0;
        if ( Departure >= Arrival ) {
            System.out.println("The times you entered seemed to be incorrect.");
            // I want to stop here...
        }
        difference = Arrival - Departure;
        return difference;
    }
Now say, if the departure time is greater than the arrival time, how would I stop the application completely from processing any further?

Thanks,
BWiz.
__________________
BWiz :: Happy Coding!
2006
2007 2008 2009
2010 2011
Irrational numbers make no sense.
BWiz is offline   Reply With Quote
Old 09-25-2008, 07:29 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
throw an exception...or if you really want to terminate use System.exit()
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 09-25-2008, 11:37 PM   PM User | #3
BWiz
Regular Coder

 
BWiz's Avatar
 
Join Date: Mar 2006
Location: Sol System
Posts: 471
Thanks: 7
Thanked 21 Times in 21 Posts
BWiz is an unknown quantity at this point
What exception would I throw?
__________________
BWiz :: Happy Coding!
2006
2007 2008 2009
2010 2011
Irrational numbers make no sense.
BWiz is offline   Reply With Quote
Old 09-26-2008, 05:26 PM   PM User | #4
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Quote:
Originally Posted by BWiz View Post
What exception would I throw?
anything that is logical in that particular situation...IllegalArgumentException comes close...
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Old 09-26-2008, 09:56 PM   PM User | #5
ess
Regular Coder

 
Join Date: Oct 2006
Location: United Kingdom
Posts: 865
Thanks: 7
Thanked 29 Times in 28 Posts
ess will become famous soon enough
You can always create class that inherits from the exception class or IllegalArgumentException class for that matter...which explicitly describes the business logics of this requirement where dates are not entered correctly by the end user.

Doing so guarantees that you are able to track errors, log them appropriately, and inform users with correct error messages whilst using your custom application.

Cheers
~E
ess is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:21 AM.


Advertisement
Log in to turn off these ads.