import java.util.Scanner; /** * A program to demo basics of exception handling structures. * * @author Lisa C. Kaczmarczyk. * Created Sep 5, 2006. */ public class NutritionalIntake { /** * Driver only. * * @param args */ public static void main(String[] args) { NutritionalIntake.getWeekday(); } /** * Tell the user what they get to eat based upon how well they follow directions. * * @param args */ public static void getWeekday() { // find out if it is a weekday and reward the user accordingly. Scanner scannerObject = new Scanner(System.in); int day; System.out.println("Use a number to tell me what weekday it is:"); System.out.println("1 - Monday, 2-Tuesday, 3-Wednesday, 4-Thursday, 5-Friday"); day = scannerObject.nextInt(); processIt(day); } /** * Process the day entered by the user by giving an encouraging (or not-so- * encouraging message). * * @param day * */ private static void processIt(int day) { try { if (day == 6 || day == 7) throw new Exception("Are you trying to say it is the weekend???"); else if (day > 7) throw new Exception("There aren't that many days in a week"); else if (day < 1) throw new Exception("Um...That makes no sense"); System.out.println("Because it is a weekday you get to eat gummy bears. Good Job!"); } catch(Exception e){ String problem = e.getMessage(); System.out.println(problem + " - you have to eat brussel sprouts"); } finally{ System.out.println("Weekdays are suddenly looking much more appealing, aren't they?"); } } }