Stuck on instanceOf - ArrayList - Java

Jon855

[H]ard DCOTM January 2008
Joined
Aug 25, 2005
Messages
11,812
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.


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());
			}
		}
 
Nemezer said:
WTH is shop(i)?
shop is the name of my ArrayList as I've indicated before showing the code and I'm not posting my entire code... I'm just posting the portion that I'm having problem.
 
Well, yeah, I was just trying to point out one of your trouble.

Also, you may want to check your spelling, it doens't look very good. (instanceOf?)
 
Showing the actual code would probably be helpful. As in, does your actual code have "instanceOf" (which is what you posted, and is wrong) or "instanceof"?

toString() is a method in Object. You don't need to cast an object to call it's toString() method.
 
javac *.java
PetStore.java:74: cannot find symbol
symbol : method shop(int)
location: class PetStore
if(shop(i) instanceof Dog)
^
PetStore.java:76: cannot find symbol
symbol : method shop(int)
location: class PetStore
Dog p = (Dog)shop(i);
^
PetStore.java:79: cannot find symbol
symbol : method shop(int)
location: class PetStore
else if(shop(i) instanceof Cat)
^
PetStore.java:81: cannot find symbol
symbol : method shop(int)
location: class PetStore
Cat p = (Cat)shop(i);
^
Note: PetStore.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
4 errors


thanks for pointing that out instanceOf != instanceof although I don't get what you're trying to tell me about the array "shop"

Please be more clear, thanks regardless.
 
SlooowJ said:
Showing the actual code would probably be helpful. As in, does your actual code have "instanceOf" (which is what you posted, and is wrong) or "instanceof"?

toString() is a method in Object. You don't need to cast an object to call it's toString() method.

But wouldn't I need to cast it back into the proper object as seeing that I'm extracting it from the ArrayList?

You want my entire code - Here you go. I've already edited the instanceOf to instanceof prior to reading your post.

Code:
//PetStore
//
import java.util.*;

public class PetStore
{
	//psvm
	public static void main(String[] args)
	{
		//Scanner start up
		Scanner scan = new Scanner(System.in);
		
		//ArrayList
		ArrayList shop = new ArrayList();
		
		//Local attributes for the customer/store
		String petChoice;
		String temp;
		boolean morePet = false;
		
		do{
			//Welcome message
			System.out.print("Welcome to Jon855's Pet Store\n" +
				"Select either Cat or Dog or exit the store\n" +
				"c/C - Cat\n" +
				"d/D - Dog\n" +
				"n/N - Exit the store\n" + 
				"Your choice please: ");
			
			//Userchoice - c/d/n
			petChoice = scan.next();
			
			//If statement to determine the choice / to create new obj of choice or exit
			//Exits the program if n/N were selected
			if(petChoice.equalsIgnoreCase("n"))
			{
				System.exit(0);
			}
			
			//Creates a new obj of Dog and adds it to the array
			if(petChoice.equalsIgnoreCase("d"))
			{
				//Creates a dog object
				Dog d = new Dog();
				
				//Adds to the array
				shop.add(d);
			}
			
			//Creates a new obj of Cat and adds it to the array
			if(petChoice.equalsIgnoreCase("c"))
			{
				//Creates a cat object
				Cat c = new Cat();
				
				//Adds to the array
				shop.add(c);
			}
			
			//Asks the user if the user wants any more pet
			System.out.print("\nDo you want to buy another pet? (y/n): ");
				temp = scan.next();
				if(temp.equalsIgnoreCase("y"))
					morePet = true;
				
		}while(morePet = true);
		
		//New pet info
		System.out.println("\nHere is/are your new pet(s)");
		
		//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());
			}
		}
	}		
}
 
You really should post your actual code.

Is shop an array or an ArrayList?

If it's an array you access the members like this: shop

If it's an ArrayList you access the members like this: shop.get(i)

shop(i) means "call a function named shop, passing the argument i".
 
Jon855 said:
But wouldn't I need to cast it back into the proper object as seeing that I'm extracting it from the ArrayList?

No, polymorphism takes care of that for you.
 
SlooowJ said:
You really should post your actual code.

Is shop an array or an ArrayList?

If it's an array you access the members like this: shop

If it's an ArrayList you access the members like this: shop.get(i)

shop(i) means "call a function named shop, passing the argument i".

It's ArrayList as I've said in my Op

now I get it. All I had to do was add the retarded .get to the ArrayList...
 
Thank you for your help, I've edited some of my other mistakes, the code is now running as it should. I don;t know why I didn't think of checking the shop.get(i) and the instanceof. At least I was somewhere close. Thanks again.
 
If you are coding Java, you really should be using an IDE which will point out these errors for you. Most of the java questions I see on here are errors which an IDE would point out and possibly correct for you.
 
generelz said:
If you are coding Java, you really should be using an IDE which will point out these errors for you. Most of the java questions I see on here are errors which an IDE would point out and possibly correct for you.
What would be a good IDE program that's free. To date I've been using Notepad++, the only thing that I've ever used was plain notepad and this notepad++. Any suggestions would be welcomed.
 
LuminaryJanitor said:

Jcreator is free, very simple to use, and fast.

netbeans is also pretty easy to use and free, although a little slower than Jcreator.

Jcreator was written in C, netbeans runs on Java (like so many of us).
 
Jon855 said:
What would be a good IDE program that's free. To date I've been using Notepad++, the only thing that I've ever used was plain notepad and this notepad++. Any suggestions would be welcomed.

I would also recommend Eclipse. I use it every day professionally and at home working on hobby projects.
 
Back
Top