Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts

    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.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #3
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    What exception would I throw?
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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

  • #5
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •