Results 1 to 4 of 4
  1. #1
    tngo is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Question instanceof question

    I am trying to check if the cell contains an object of type Anteater using instanceof. However, when i tried to compile it I get an error message stating

    WorldOfAntsSim.java:391: illegal start of expression
    if ((y>0) && (world.getAt(x, y-1) != instanceof Anteater))
    ^
    WorldOfAntsSim.java:393: illegal start of expression
    if (world.getAt(x, y-1) == instanceof Ant)
    ^
    Am I going about this all wrong? Thank you for any advice you can render.



    Java Code:
    public void move()			// Must define this since virtual
    	{
    		// Pick random direction to move
    		int dir = (int) (Math.random() * 4);
    		// Try to move up, if not at an edge and empty spot
    		if (dir==0)
    		{
    			if ((y>0) && (world.getAt(x, y-1) != instanceof Anteater))
    			{
    				 if (world.getAt(x, y-1) == instanceof Ant)
    				 {
    					 world.setAt(x,y-1,world.getAt(x,y));  // Move to new spot
    					 world.setAt(x,y,null);
    					 y--;
    					 breedTicks++;
    				 }
    				 else
    				 {
    					 world.setAt(x,y-1,world.getAt(x,y));  // Move to new spot
    					 world.setAt(x,y,null);
    					 y--;
    					 breedTicks++;
    					 untilStarve--;
    				 }
    			}

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: instanceof question

    Hi tngo,

    Try this:

    Java Code:
    if ((y>0) && !(world.getAt(x, y-1) instanceof Anteater))
    Regards.

  3. #3
    tngo is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: instanceof question

    Quote Originally Posted by Ronin View Post
    Hi tngo,

    Try this:

    Java Code:
    if ((y>0) && !(world.getAt(x, y-1) instanceof Anteater))
    Regards.
    Ah, I understand. It returns a boolean value. Thank you so very much.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

Similar Threads

  1. Instanceof
    By KevinNguyen in forum New To Java
    Replies: 12
    Last Post: 11-08-2012, 05:55 PM
  2. instanceof
    By AedonetLIRA in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 12:34 AM
  3. Class, SubClass type (InstanceOf) Question
    By indyjoel in forum New To Java
    Replies: 6
    Last Post: 11-07-2010, 03:55 AM
  4. Use of keyword instanceof
    By darek9576 in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 10:35 PM
  5. super instanceof Class?
    By mikeiz404 in forum New To Java
    Replies: 11
    Last Post: 01-23-2009, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •