I'm trying to check for the object inside the ArrayList and for some reason I've not been able to get the instanceOf to work, I know I've made mistake somewhere. This is pissing me off to no ends.
shop = ArrayList that holds the below objects
Dog and Cat are the object names
Objects are stored in the shop ArrayList. I need to extract the object and cast it so that I can call the toString method within that casted object.
Everything works but the instanceOf, I need to set it up so that when i'm searching for the Object, I want it to be able to find Dog or Cat and that's where I'm stuck at right now. I must have used the instanceOf in a manner that's not working the code in question is : if(shop(i) instanceOf Dog)
Any help in getting the instanceOf to work would be great. Thanks.
shop = ArrayList that holds the below objects
Dog and Cat are the object names
Objects are stored in the shop ArrayList. I need to extract the object and cast it so that I can call the toString method within that casted object.
Everything works but the instanceOf, I need to set it up so that when i'm searching for the Object, I want it to be able to find Dog or Cat and that's where I'm stuck at right now. I must have used the instanceOf in a manner that's not working the code in question is : if(shop(i) instanceOf Dog)
Any help in getting the instanceOf to work would be great. Thanks.
Code:
//Loops through the ArrayList and prints out the toString from each respectfully object inside the array
for(int i = 0; i > shop.size(); i++)
{
if(shop(i) instanceOf Dog)
{
Dog p = (Dog)shop(i);
System.out.println(p.toString());
}
else if(shop(i) instanceOf Cat)
{
Cat p = (Cat)shop(i);
System.out.println(p.toString());
}
}