Results 1 to 7 of 7
  1. #1
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Having some array trouble

    Hello Everyone, I'm writing a program for my class that involves storing some objects in arrays, and I'm trying to use afor loop to quickly draw all the objects stored in the array, here is the important code:

    Declaration:

    public Velociraptor[] raptors;

    init()

    raptors = new Velociraptor[5];

    raptors[0] = new Velociraptor();
    raptors[1] = new Velociraptor();
    raptors[2] = new Velociraptor();
    raptors[3] = new Velociraptor();
    raptors[4] = new Velociraptor();

    paint()

    for (int i=0; i<=5; i++)
    {
    if(raptors[i].isAlive==true)
    {
    g.drawImage(raptorPic,raptors[i].xpos,raptors[i].ypos,this);
    raptors[i].move();
    }
    }

    The problem is when I try to run it I get a cannot find symbol (i) error, I'm not sure what's wrong. Hopefully one of you can help, thanks in advance.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Having some array trouble

    The full compiler message would help. And an indication of what line of your code it is referring to.

    Note: use "code" tags when posting code or compiler messages. Put [code] at the start of the code and [/code] at the end: that way the formatting is preserved when the code is rendered on a web page. On a good day the font will also be fixed so that we can see where the compiler's little ^ symbol is pointing.

  3. #3
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Having some array trouble

    ForestWorld1.java:106: cannot find symbol
    symbol : variable i
    location: class ForestWorld1
    if(Gregory.rec.intersects(raptors[i].rec)&&raptors[i].isAlive==true)

    There is a "^" under the first [i], but I couldn't get the formatting to do it
    Above is one of the errors, there are six in all pertaining to these intercepting rectangles if statements here is one of them

    Java Code:
     if(Gregory.rec.intersects(raptors[i].rec)&&raptors[i].isAlive==true)
    		{
    			raptors[i].isAlive=false;
    		}
    All the "i"s get the same error, and there is a second if statement that does the same thing but with a different object

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Having some array trouble

    The error is occuring on a line that has nothing to do with the code you posted orignally. The compiler is telling you the same thing. You have declared the variable i inside the loop but are trying to use it outside the loop.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Having some array trouble

    Perhaps you could post the full method. I suspect (but can't be sure) the for loop looks something like

    Java Code:
    for (int i=0; i<=5; i++); // <-- the ; on this line is a mistake
      {
        if(raptors[i].isAlive==true)
        {
            g.drawImage(raptorPic,raptors[i].xpos,raptors[i].ypos,this);
            raptors[i].move();
        }
      }
    [Edit] or what Junky says.

  6. #6
    beijct is offline Member
    Join Date
    Nov 2011
    Posts
    12
    Rep Power
    0

    Default Re: Having some array trouble

    This is the full method

    Java Code:
    public void paint(Graphics g)
    	{
    		
    		for (int i=0; i<=5; i++)
    			{
    				if(raptors[i].isAlive==true)
    					{
    						g.drawImage(raptorPic,raptors[i].xpos,raptors[i].ypos,this);
    						raptors[i].move();
    					}
    			}
    
    	if(Gregory.isAlive==true)
    		{
    		g.drawImage(philosoPic,Gregory.xpos,Gregory.ypos,this);
    
    		Gregory.move();
    		}
    
    	if(Joe.isAlive==true)
    		{
    		g.drawImage(brontoPic, Joe.xpos,Joe.ypos,this);
    
    		Brosif.move();
    		}
    	if(Gregory.rec.intersects(raptors[i].rec)&&raptors[i].isAlive==true)
    		{
    			raptors[i].isAlive=false;
    		}
    
    	if(joe.rec.intersects(raptors[i].rec)&&raptors[i].isAlive==true)
    		{
    			raptors[i].isAlive=false;
    		}
    
    
    	}

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Having some array trouble

    I'm sure I posted the answer to this problem. <scrolls up> Yep! Post #4 is visible.

Similar Threads

  1. text to array trouble
    By rat in forum New To Java
    Replies: 13
    Last Post: 05-18-2011, 06:32 PM
  2. Having trouble with 2d Array
    By geekchick in forum New To Java
    Replies: 2
    Last Post: 08-02-2010, 05:54 AM
  3. trouble with array method
    By kpro862 in forum New To Java
    Replies: 6
    Last Post: 05-12-2010, 01:08 AM
  4. Array trouble
    By hobo in forum New To Java
    Replies: 8
    Last Post: 11-17-2009, 01:04 PM
  5. Having trouble with array
    By ice22 in forum New To Java
    Replies: 3
    Last Post: 11-13-2007, 04:06 AM

Posting Permissions

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