Link to home
Start Free TrialLog in
Avatar of LearningJava
LearningJava

asked on

cast error?

Hi:
I get the following runtime error when I try to run my Calendar program:

java.lang.ClassCastException
at Calendar.dupsTest(CalendarTest.java, Compiled Code)
at Calendar.print(CalendarTest.java, Compiled Code)
at CalendarTest.main(CalendarTest.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(toFind);    
     
      //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)appointments.get(i)).equals(toFind))
         {
         
             appointments.remove(i);
         }
      }
           
   }
   
   /**
   Tests for duplicate appointment dates.
   */
   public void dupsTest()
   {  
      for (int x = 0; x < appointments.size(); x++)
      {
         String cmp = (String)appointments.elementAt(x);
         for (int y = appointments.size()-1; y > x; y --)
         {
            if (cmp.equals((Appointment)appointments.elementAt(y)))
            System.out.println("duplicates");
         }
   
      }
   
   }  
   
   
   
   
 

   /**
   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.starttime)
                 && endtime.equals(other.endtime) && 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;

}
Avatar of Venci75
Venci75

Why is this line:
String cmp = (String)appointments.elementAt(x);

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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you have 2 instructions to check :
1 String cmp = (String)appointments.elementAt(x);
2 if (cmp.equals((Appointment)appointments.elementAt(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
Avatar of LearningJava

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...