0

Hi im trying to create a code where i get user to input date. I will then manipulate this date to create a cost of travel for each day. Im struggling to add Exceptions to prevent errors from being entered. Can anyone give me some tips on how to do this? My Code:

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Price
{
  public static void main (String [] args)
  {
    userInput();         
  }

  public static void userInput()
  {
    Scanner scan = new Scanner(System.in);

    int month, day, year;

    System.out.println("Please enter a month MM: ");
    month = scan.nextInt();
    System.out.println("Please enter a day DD: ");
    day = scan.nextInt();
    System.out.println("Please enter a year YYYY: ");
    year = scan.nextInt();
    System.out.println("You chose: " + month + " /" + day +  " /" + year);  
  }
}
1
  • 2
    This shouldn't be handled by exceptions, but by simple tests/loops. See hasNextInt() in the Scanner javadoc. Commented Mar 21, 2013 at 22:27

3 Answers 3

1

Hide the exception handling inside a method...

public static int inputInteger(Scanner in, String msg, int min, int max) {
  int tries = 0;
  while (tries < maxTries) {
    System.out.println(msg);
    try {
      int result = in.nextInt();
      if (result < min || result > max) {
        System.err.println("Input out of range:" + result);
        continue;
      }
      return result;
    } catch (Exception ex) {
      System.err.println("Problem getting input: "+ ex.getMessage());
    }
  }
  throw new Error("Max Retries reached, giving up");
}

This is a little simplistic, but it's a good start for simple apps. The same sort of loop can allow you to validate the input (e.g. don't take 35 as a date)

Sign up to request clarification or add additional context in comments.

Comments

0

May be you should use IllegalArgumentException like this:

if (month < 1 || month > 12 || day < 1 || day > 31)
    throw new IllegalArgumentException("Wrong date input");

or Exception base class:

if (month < 1 || month > 12 || day < 1 || day > 31)
    throw new Exception("Wrong date input");

Also, you can create your own subclass of Exception:

class WrongDateException extends Exception
{
   //You can store as much exception info as you need
}

and then catch it by

try {
    if (!everythingIsOk)
        throw new WrongDateException();
}
catch (WrongDateException e) { 
    ... 
}

Comments

0

I will just put the first loop which asking for month and the others is the same steps and the same idea see this :

int month, day, year;
while(true){
    try{
System.out.println("Please enter a month MM: ");
month=scan.nextInt();
if(month>0&&month<=12)
    break;
else System.err.println("Month should be between 1 and 12");
}catch(InputMismatchException ex){
   System.err.println(ex.getMessage());
     }
}
System.out.println("You chose: " + month );

2 Comments

Scanners throw WrongInputException.
Any way NumberFormatException or InputMismatchException must be handled here for not breaking the loop due to the mistmatch disaster error :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.