/******************************* MAIN HEADER ********************************** Title: Oops4 Date Exception Handling File: Oops4.java Author: James D. Skrentny, skrentny@cs.wisc.edu copyright 2000 all rights reserved Course: CS 302: Lectures 1 & 2 Compiler: CodeWarrior IDE 4.0 (JDK 1.2) Platform: Windows NT 4.0 **************************** 80 columns wide *********************************/ import java.io.*; import java.util.StringTokenizer; /** * This program demonstrates how to catch an exception that we * created - InvalidDateException. **/ class Oops4 { // allows stdin to be shared private static BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); // driver program public static void main (String[ ] args) { Date start = getDate("\n\nEnter start date."); System.out.println(start); Date stop = getDate("\n\nEnter stop date."); System.out.println(stop); } /** * A general input method to read a date. * @param prompt user prompt * @return the date inputted **/ private static Date getDate (String prompt) { System.out.println(prompt); int day = 0, year = 0; String month = null; // require input until a valid date is entered while (true) { try { day = getInt ("Enter day of month [1-31]: ", 1, 31); month = getWord("Enter month [e.g. Jan]: "); year = getInt ("Enter year [>0]: ", 1, Integer.MAX_VALUE); } catch (IOException oops) { System.err.println("Error #2: cute message"); System.err.println(oops.getMessage()); oops.printStackTrace(); System.exit(1); } //could combine this with try statement above try { return new Date(day, month, year); } //catch the exception we created catch (InvalidDateException oops) { System.out.print("INVALID date: "); switch (oops.getType()) { case 1: System.out.println("bad day value"); break; case 2: System.out.println("bad month value"); break; case 3: System.out.println("bad year value"); break; default: System.out.println("unknown"); break; } } } } /** * A general input method to read an integer within specified limits. * @param prompt user prompt * @param min specified lower limit * @param max specified upper limit * @return the integer inputted **/ private static int getInt (String prompt, int min, int max) throws IOException { boolean repeat; int i = 0; do { repeat = false; // assume user correctly enters # > 0 // get input System.out.println(prompt); String s = stdin.readLine(); // try to parse to integer and verify within limits try { i = Integer.parseInt(s); if (i < min || i > max) { System.out.println(i + " is INVALID! must be from " + min + " to " + max + "\n\n"); repeat = true; } } // handle invalid number format (i.e. catch locally) catch (NumberFormatException oops) { System.out.println(s + " is INVALID! must be an integer\n\n"); repeat = true; } } while (repeat); return i; } /** * A general input method a single word. * @param prompt user prompt * @return the one word inputted **/ private static String getWord (String prompt) throws IOException { boolean repeat; String s; do { repeat = false; // assume user correctly enters one word // get input System.out.println(prompt); s = stdin.readLine(); // count tokens StringTokenizer t = new StringTokenizer(s, " \t"); if (t.countTokens() > 1) { System.out.println(s + " is INVALID! must be 1 word"); repeat = true; } } while (repeat); return s; } }