This discussion is archived
10 Replies Latest reply: Aug 23, 2007 9:08 AM by Puce RSS

class cast exception

807605 Newbie
Currently Being Moderated
Hi im a bit confused at what a classCastException is and what it has to do with this code could any one explane and help if possable thanks :)

foundRooms = checkRooms(roomNum,floorNum);

private int checkRooms(int roomNum,int floorNum) {

int coin = 0; // i use coin as yes/no

Iterator iter = h.getFloorIterator(floorNum); // h means hotel class

while (iter.hasNext() == true) {
Room r = (Room) iter.next();

if ((roomNum == r.getRoomNum()) && (floorNum == r.getFloorNum())) {
coin = 1;
}

if ((roomNum != r.getRoomNum()) && (floorNum != r.getFloorNum())) {
     coin = 0;
     }
}

if ((iter.hasNext() == false) && (coin == 0)) {System.out.print("");
     }
return coin;
}

this is in the hotel class

private Collection floor1, floor2, floor3;

public Hotel() {
     floor1 = new ArrayList();
     floor2 = new ArrayList();
     floor3 = new ArrayList();
}

public Iterator getFloorIterator(int floorNum) {
     switch(floorNum) {
     case 1: return floor1.iterator();
     case 2: return floor2.iterator();
     case 3: return floor3.iterator();
     default: System.out.print("");
     }
return null;
}
  • 1. Re: class cast exception
    Puce Newbie
    Currently Being Moderated
    A ClassCastException gets thrown when the casted object is not an instance of (directly or indirectly extends or implements) the casted type. Eg.:
    Room r = (Room) iter.next();
    This line will throw a ClassCastException if
    (iter.next() instanceof Room) == false
    -Puce
  • 2. Re: class cast exception
    807605 Newbie
    Currently Being Moderated
    If you were putting up some shelves and asked me to hand you a screwdriver, but I handed you a donut instead, and promised it was a screwdriver, you might throw a ClassCastException when you discovered the truth

    At a guess, your iterator is not iterating over a collection of Rooms

    Incidentally, you aren't in 'C' land now, you've got a proper boolean type to use instead of int for your coin! And iter.hasNext() returns a boolean, so you don't need to compare the result to true, just use while(iter.hasNext())
  • 3. Re: class cast exception
    807605 Newbie
    Currently Being Moderated
    This line throws ClassCastException if next() returns an object which doesn't have type Room:
    Room r = (Room) iter.next();
    The best way to fix this is to obviate the need for casting, by using generics:

    http://java.sun.com/docs/books/tutorial/collections/index.html
  • 4. Re: class cast exception
    Puce Newbie
    Currently Being Moderated
    What did you add to your floor collections? Rooms? Only Rooms?

    -Puce
  • 5. Re: class cast exception
    Puce Newbie
    Currently Being Moderated
    The best way to fix this is to obviate the need for
    casting, by using generics:

    http://java.sun.com/docs/books/tutorial/collections/in
    dex.html
    I agree. If you use jdk 5 or greater you could use something like:
    private final Collection<Room> floor1; 
    private final Collection<Room> floor2;
    private final Collection<Room> floor3;
    
    public Hotel() {
    floor1 = new ArrayList<Room>();
    floor2 = new ArrayList<Room>();
    floor3 = new ArrayList<Room>();
    }
    This way the compiler will tell you, where the problem is (instead of letting you run into a runtime exception).

    -Puce
  • 6. Re: class cast exception
    807605 Newbie
    Currently Being Moderated
    Ok here is the code that adds the rooms

    private Collection floor1, floor2, floor3;

    public Hotel() {
         floor1 = new ArrayList();
         floor2 = new ArrayList();
         floor3 = new ArrayList();
    }

    public void addRoom(int floorNum, int roomNum, int MaxOccupancy, String roomType) {

    Room rm = new Room(floorNum,roomNum,MaxOccupancy,roomType);
              
         switch(floorNum) {
         case 1: floor1.add(rm); break;
         case 2: floor2.add(rm); break;
         case 3: floor3.add(rm); break;
         default: System.out.print("");
         }
    }

    and here is the room class

    public class Room {
         
    private int
    floorNum, // the floor number / the number of the floor
    maxOccupancy, // the maximum amount of people allowed in at once
    roomNum; // the room number / the number of the room
         
    private String
    roomType; // the type of room (e.g. bed study conference)

    public int getFloorNum() {
         return floorNum;
    }

    public int getRoomNum() {
         return roomNum;
    }

    public String getRoomType() {
         return roomType;
    }

    public int getMaxOccupancy() {
         return maxOccupancy;
    }
         
    public String toString() {
    return "Floor number: "+floorNum+"\n" +
    "Room number: "+roomNum+"\n" +
    "Room type: "+roomType+"\n" +
    "Max occupancy: "+maxOccupancy+"\n";
    }
         
    public Room(int floorNum, int roomNum, int maxOccupancy, String roomType) {
         this.floorNum = floorNum;
         this.roomNum = roomNum;
         this.roomType = roomType;
         this.maxOccupancy = maxOccupancy;
         }
    }

    is there any thing wrong with them ? :)

    Message was edited by:
    headlice1
  • 7. Re: class cast exception
    807605 Newbie
    Currently Being Moderated
    Try this
    Object object = iter.next();
    System.out.println(object.getClass().getName());
    See what it prints out
  • 8. Re: class cast exception
    807605 Newbie
    Currently Being Moderated
    ok
  • 9. Re: class cast exception
    807605 Newbie
    Currently Being Moderated
    cool ive got my rooms back thanks georgemc dewd lol

    Message was edited by:
    headlice1
  • 10. Re: class cast exception
    807605 Newbie
    Currently Being Moderated
    Incidentally, you aren't in 'C' land now, you've got
    a proper boolean type to use instead of int for your
    coin! And iter.hasNext() returns a boolean, so you
    don't need to compare the result to true, just use
    while(iter.hasNext())
    And what's with the System.out.print("") all over. Could replace that with say
    // don't know what to do here so I'll just print out nothing