5

I am writing an appointment program and am getting the following errors:

AppointmentNew.java:68: unreported exception java.text.ParseException; must be caught or declared to be thrown
        Date lowDate = sdf.parse(stdin.nextLine());
                                ^
AppointmentNew.java:70: unreported exception java.text.ParseException; must be caught or declared to be thrown
        Date highDate = sdf.parse(stdin.nextLine());  
                                 ^
AppointmentNew.java:77: unreported exception java.text.ParseException; must be caught or declared to be thrown
           Date newCurrentDate = sdf.parse(currentDate); 

I am pretty sure I need to do a try/catch, but I am not sure of how to produce that. The part where I am getting the error is where it asks the user to input a BEGINNING and END date, when they do that the program then prints out the appointments they have made between the two dates.

Here is my code I have :

import java.util.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AppointmentNew 
{
public static void main (String[] args)
{
  ArrayList<String> list = new ArrayList<String>();
  Scanner stdin = new Scanner(System.in);
  String choice = "";
  int choiceNum = 0;
  String date = "";
  String descrip = "";
  int type = 0;
  String typeChose = "";

  System.out.println("Welcome to Appointment App!\n");
  System.out.println("\t============================");

  do
  {
     System.out.print("\n\tMake Choice (1: New, 2: Print Range, 3: Print All, 4: Quit) ");
     choice = stdin.nextLine();
     choiceNum = Integer.parseInt(choice);

     if (choiceNum == 1)
     {
        System.out.print("\n\n\tEnter New Appointment Date in mm/dd/yyyy format: ");
        date = stdin.nextLine();

        System.out.print("\n\n\tEnter New Appointment Description: ");
        descrip = stdin.nextLine();

        System.out.print("\n\n\tEnter Type (1 = Once, 2 = Daily, 3 = Monthly): ");
        type = stdin.nextInt();
        stdin.nextLine();

        if (type == 1)
        {
           Once once = new Once(date, descrip);
           typeChose = "One-Time";
        }
        else if (type == 2)
        {
           Daily daily = new Daily(date, descrip);
           typeChose = "Daily";
        }
        else
        {
           Monthly monthly = new Monthly(date, descrip);
           typeChose = "Monthly";
        }
        String stringToAdd = "";
        stringToAdd = (date + " : \"" + descrip + "\", " + typeChose);
        list.add(stringToAdd);

        System.out.println("\n\n\tNew " + typeChose + " Appointment Added for " + date + "\n");
        System.out.println("\t============================\n");


     }

     if (choiceNum == 2)
     {
        System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        Date lowDate = sdf.parse(stdin.nextLine());
        System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");
        Date highDate = sdf.parse(stdin.nextLine());  

        for(int i = 0; i < list.size(); i++)
        {
           int dateSpot = list.get(i).indexOf(" ");
           String currentDate = list.get(i);
           currentDate.substring(0, dateSpot);
           Date newCurrentDate = sdf.parse(currentDate); 

           if (newCurrentDate.compareTo(lowDate) >= 0 && newCurrentDate.compareTo(highDate) <= 0)
           {
              System.out.println("\n\t" + list.get(i));   
           }
        }
     }

     if (choiceNum == 3)
     {
        for(int i = 0; i < list.size(); i++)
        {
           System.out.println("\n\t" + list.get(i));     
        }
     }

  }while (choiceNum != 4);      
 }
}
Roman C
  • 47,329
  • 33
  • 60
  • 147
user2213611
  • 91
  • 2
  • 4
  • 13
  • 3
    You've shown no sign of doing research in the 10 minutes since you asked about this problem in a comment on another question. Did you read a tutorial on exception handling? If so, which exact bit of it did you not understand? You say you think you ought to use a try/catch block but don't know how to do it - which bit of that is causing you problems? What have you tried? – Jon Skeet Apr 19 '13 at 20:05
  • Yes I have done research, I went to "http://docs.oracle.com/javase/tutorial/essential/exceptions/try.html" and read about how to do a try block. I am not sure of how to start it. – user2213611 Apr 19 '13 at 20:13
  • 1) try {Date lowDate = sdf.parse(stdin.nextLine()); } catch(ParseException e){}. OR 2) public static void main(String...args) throws ParseException {} – NINCOMPOOP Apr 19 '13 at 20:16
  • @user2213611: You need to read more than one page. It's a tutorial, teaching you the concepts step by step. Read the *whole* of http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html – Jon Skeet Apr 19 '13 at 20:19
  • Do you use an IDE at all? They normally show you the error and can offer solutions to you. If you're still fairly new though it's best to learn how to deal with them. Exceptions aren't hard, just take getting used to. – eldris Apr 19 '13 at 21:53

1 Answers1

13

The format is:

try {
 //The code you are trying to exception handle
}
catch (Exception e) {
 //The handling for the code
}

Put the parse calls in a try block (preferably each in their own), and then specify in the catch block what should happen if the parsing fails.

Petter Friberg
  • 19,652
  • 9
  • 51
  • 94
Chris Chambers
  • 1,337
  • 20
  • 37
  • What would I put for the "handling" code? I know what to put for the "try" code. – user2213611 Apr 19 '13 at 21:41
  • Well, probably set the type to default (maybe type = 0). Basically, what should the program do if it reads in something that cannot be converted to an int. It's a nicer way of doing things than just throwing up a big error message and crashing. – Chris Chambers Apr 19 '13 at 21:49