LearningJava
asked on
cast error?
Hi:
I get the following runtime error when I try to run my Calendar program:
java.lang.ClassCastExcepti on
at Calendar.dupsTest(Calendar Test.java, Compiled Code)
at Calendar.print(CalendarTes t.java, Compiled Code)
at CalendarTest.main(Calendar Test.java: 20)
Exception in thread "main" Process Exit...
I know the problem occurs in the dupTest method but I am not sure how to correct it. Can someone help?
Thanks.
/**
Describes a calendar for a set of appointments.
@version 1.0
*/
import java.util.Vector;
public class CalendarTest
{ public static void main(String[] args)
{
Calendar markCalendar = new Calendar("Mark");
markCalendar.addApp(new Appointment("June 1","3pm","4pm", "dentist"));
markCalendar.addApp(new Appointment("June 2","3pm","4pm", "doctor"));
markCalendar.print();
Appointment toFind = new Appointment("June 2","3pm","4pm", "doctor");
markCalendar.removeApp(toF ind);
//markCalendar.addApp(new Appointment("June 2","3pm","4pm", "lawyer"));
//markCalendar.print();
markCalendar.print();
}
}
/**
Describes a calendar for a set of appointments.
*/
class Calendar
{
/**
Constructs a calendar for the person named.
*/
public Calendar(String aName)
{ name = aName;
appointments = new Vector();
}
/**
Adds an appointment to this Calendar.
@param anApp The appointment to add.
*/
public void addApp(Appointment anApp)
{
appointments.add(anApp);
}
/**
Removes an appointment from this Calendar.
@param anApp The appointment to be removed.
*/
public void removeApp(Appointment toFind)
{
for ( int i = 0; i < appointments.size(); i++)
{
if (((Appointment)appointment s.get(i)). equals(toF ind))
{
appointments.remove(i);
}
}
}
/**
Tests for duplicate appointment dates.
*/
public void dupsTest()
{
for (int x = 0; x < appointments.size(); x++)
{
String cmp = (String)appointments.eleme ntAt(x);
for (int y = appointments.size()-1; y > x; y --)
{
if (cmp.equals((Appointment)a ppointment s.elementA t(y)))
System.out.println("duplic ates");
}
}
}
/**
Prints the Calendar.
*/
public void print()
{ System.out.println(name + " C A L E N D A R");
System.out.println();
System.out.println("Date Starttime EndTime Appointment");
for (int i = 0; i < appointments.size(); i++)
{ Appointment nextApp =(Appointment) appointments.get(i);
nextApp.print();
}
dupsTest();
}
private Vector appointments;
private String name;
private Appointment theAppointment;
}
/**
Describes an appointment.
*/
class Appointment
{
public Appointment(String aDate,String aStarttime,String aEndtime, String aApp)
{ date = aDate;
starttime = aStarttime;
endtime = aEndtime;
app = aApp;
}
/**
Method to test whether on object equals another.
@param otherObject The other object.
@return true if equal, false if not
*/
public boolean equals(Object otherObject)
{
if (otherObject instanceof Appointment)
{ Appointment other = (Appointment)otherObject;
return (date.equals(other.date) && starttime.equals(other.sta rttime)
&& endtime.equals(other.endti me) && app.equals(other.app));
}
else return false;
}
/**
Prints the Date, Starttime, Endtime and a description of the
appointment.
*/
public void print()
{ System.out.println();
System.out.println(date + " " + starttime + " " + endtime
+ " " + app );
System.out.println();
}
private String date;
private String starttime;
private String endtime;
private String app;
}
I get the following runtime error when I try to run my Calendar program:
java.lang.ClassCastExcepti
at Calendar.dupsTest(Calendar
at Calendar.print(CalendarTes
at CalendarTest.main(Calendar
Exception in thread "main" Process Exit...
I know the problem occurs in the dupTest method but I am not sure how to correct it. Can someone help?
Thanks.
/**
Describes a calendar for a set of appointments.
@version 1.0
*/
import java.util.Vector;
public class CalendarTest
{ public static void main(String[] args)
{
Calendar markCalendar = new Calendar("Mark");
markCalendar.addApp(new Appointment("June 1","3pm","4pm", "dentist"));
markCalendar.addApp(new Appointment("June 2","3pm","4pm", "doctor"));
markCalendar.print();
Appointment toFind = new Appointment("June 2","3pm","4pm", "doctor");
markCalendar.removeApp(toF
//markCalendar.addApp(new Appointment("June 2","3pm","4pm", "lawyer"));
//markCalendar.print();
markCalendar.print();
}
}
/**
Describes a calendar for a set of appointments.
*/
class Calendar
{
/**
Constructs a calendar for the person named.
*/
public Calendar(String aName)
{ name = aName;
appointments = new Vector();
}
/**
Adds an appointment to this Calendar.
@param anApp The appointment to add.
*/
public void addApp(Appointment anApp)
{
appointments.add(anApp);
}
/**
Removes an appointment from this Calendar.
@param anApp The appointment to be removed.
*/
public void removeApp(Appointment toFind)
{
for ( int i = 0; i < appointments.size(); i++)
{
if (((Appointment)appointment
{
appointments.remove(i);
}
}
}
/**
Tests for duplicate appointment dates.
*/
public void dupsTest()
{
for (int x = 0; x < appointments.size(); x++)
{
String cmp = (String)appointments.eleme
for (int y = appointments.size()-1; y > x; y --)
{
if (cmp.equals((Appointment)a
System.out.println("duplic
}
}
}
/**
Prints the Calendar.
*/
public void print()
{ System.out.println(name + " C A L E N D A R");
System.out.println();
System.out.println("Date Starttime EndTime Appointment");
for (int i = 0; i < appointments.size(); i++)
{ Appointment nextApp =(Appointment) appointments.get(i);
nextApp.print();
}
dupsTest();
}
private Vector appointments;
private String name;
private Appointment theAppointment;
}
/**
Describes an appointment.
*/
class Appointment
{
public Appointment(String aDate,String aStarttime,String aEndtime, String aApp)
{ date = aDate;
starttime = aStarttime;
endtime = aEndtime;
app = aApp;
}
/**
Method to test whether on object equals another.
@param otherObject The other object.
@return true if equal, false if not
*/
public boolean equals(Object otherObject)
{
if (otherObject instanceof Appointment)
{ Appointment other = (Appointment)otherObject;
return (date.equals(other.date) && starttime.equals(other.sta
&& endtime.equals(other.endti
}
else return false;
}
/**
Prints the Date, Starttime, Endtime and a description of the
appointment.
*/
public void print()
{ System.out.println();
System.out.println(date + " " + starttime + " " + endtime
+ " " + app );
System.out.println();
}
private String date;
private String starttime;
private String endtime;
private String app;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you have 2 instructions to check :
1 String cmp = (String)appointments.eleme ntAt(x);
2 if (cmp.equals((Appointment)a ppointment s.elementA t(y)))
Where casting failed since the object inside the Vector is
not matching the Class type you're casting.
You may use a standard debugger to debug that or use the
instanceof to check for type
1 String cmp = (String)appointments.eleme
2 if (cmp.equals((Appointment)a
Where casting failed since the object inside the Vector is
not matching the Class type you're casting.
You may use a standard debugger to debug that or use the
instanceof to check for type
ASKER
I figured this one out myself but you still get the points for posting the answer.
Thanks, but they should have gone to Venci75...
String cmp = (String)appointments.eleme
in the
public void dupsTest()
the elements of the vector are not strings - they are Appointment objects. Alsp - I can't see where you use the cmp string