Reputation: 91
I am writing an appointment program in Java and am coming across an error which is as follows :
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);
This program is suppose to allow the user to make new appointments, when they do that they are then able to input a date and description (they can do this as many times as they want), after that they can then pick a range of dates for the program to print out (it is suppose to print out the appointments in the date range they provided". This is where my error occurs...
Here is the 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);
}
}
Upvotes: 0
Views: 8238
Reputation: 635
It is normal, you compare a object of Type Date with a String. Convert your String to a Date with your SimpleDateFormat (or with another format) and you will able to compare both objects.
Date correctCurrentDate = sdf.parse(currentDate);
To avoid to add try/catch in test program, I generally wrote the main method like :
public static void main (String[] args) throws Exception
Of course, for program that run in production, you should use try/catch where you can handle the exception. But it is annoying for a test program.
Upvotes: 1
Reputation: 1500495
The problem is that you're trying to compare a string to a date. What does that even mean?
You should convert the String
to a Date
(e.g. with SimpleDateFormat
) - or ideally, use Joda Time - and then compare the values when you've got them both as the same type.
EDIT: As noted in comments, this isn't what you want either:
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
mm
means minutes in SimpleDateFormat
, not months. You want MM/dd/yyyy
instead. See the SimpleDateFormat
documentation for more details.
Upvotes: 7
Reputation: 66637
currentDate.compareTo(lowDate)
currentDate
is String
and lowDate
is Date
. You are trying compareTo
on two different things.
Convert currentDate
to Date object and then call compareTo();
Upvotes: 2