Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • Last Modified:

Array index bounds

I have a code that utilises 2 arrays X and Y, each with 4 sub arrays and includes a for loop to cycle through the arrays and a if statement to reset the loop counters for the next sub array. However, when I try to compile it I get a runtime 'array index out of bounds' error. I can't work out what is wrong with the code, although it may be in the if statement? It is included below:

//Declared as class variables

ArrayXCo-ords[]={
{1,2,3,4},
{4,5,6,7,8,9},
{10,11,12,13,14,15,16},
{17,18,19,20,21}
};

ArrayYCo-ords[]={
{1,2,3,4},
{4,5,6,7,8,9},
{10,11,12,13,14,15,16},
{17,18,19,20,21}
};

//Declared in paint

for (int count=0; count>ArrayXCo-ords.length;count++)
{
      myGraphics.fillPolygon(ArrayXCo-ords[i],ArrayYCo=ords[j],ArrayXCo-ords.length);

      if (count==1 && i==4)
      {
            i=0;      //Reset array index variables for next sub array
            j=0;
      }

      else if (count==2 && i==6)
      {
            i=0;
            j=0;
      }
      else if (count==3 && i==7)
      {
            i=0;
            j=0;
      }
}
0
mark_667
Asked:
mark_667
  • 8
  • 6
  • 2
1 Solution
 
CEHJCommented:
>>count>ArrayXCo-ords.length

should be

count < ArrayXCo-ords.length

for one
0
 
CEHJCommented:
But i'm wondering how you're compiling that anyway, as the hyphen makes that an invalid symbol
0
 
WebstormCommented:
Hi mark_667,

1> replace the hyphen by nothing or _
     (as CEHJ said)

2> why not using count instead of i & j ?

for (int count=0; count<ArrayXCoords.length;count++)
     myGraphics.fillPolygon(ArrayXCoords[count],ArrayYCoords[count],ArrayXCoords[count].length);

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
mark_667Author Commented:
CEHJ: You are correct but those errors are not in my code, I just transcribed it  in a hurry.

Webstorm: I don't use count except as the loop counter because then only the first 4 points of the polygon would be drawn as the array counter only goes up to 4 and all the sub arrays are longer than that. Using i and j and most importantly re-setting them after they have been incremented to the length of the sub array means the two variables will cycle through all the points in the current array sequentially.
0
 
CEHJCommented:
Please post then the code that you *are* using and tell us the status now
0
 
WebstormCommented:
>> I don't use count except as the loop counter because then only the first 4 points of the polygon would be drawn as the array counter only goes up to 4 and all the sub arrays are longer than that.

then try using count, because :

   myGraphics.fillPolygon(ArrayXCoords[count],ArrayYCoords[count],ArrayXCoords[count].length);
   -> count = 0
   myGraphics.fillPolygon(ArrayXCoords[0],ArrayYCoords[0],ArrayXCoords[0].length);
                                //          {1,2,3,4}   ,       {1,2,3,4}   ,   4
   -> count = 1
   myGraphics.fillPolygon(ArrayXCoords[1],ArrayYCoords[1],ArrayXCoords[1].length);
                                //    {4,5,6,7,8,9}   ,    {4,5,6,7,8,9} ,   6
   ...
0
 
mark_667Author Commented:
Sorry about the delay the code I'm using is:

import element.*;
import Java.awt.Color;
import Java.awt.event.*;
import Java.applet.Applet;

public class Prog_With_Irritating_Array_Problem extends Applet implements Mouse Listener

      Font myFont=new Font("Times", Font.BOLD,16);

      int [][] XArrays={{0,200,450,450,200,0},
      {200,100,75,125,135,110,200,300,400,400,310,230,325,400,410,325,315,320,230,210},
            {125,100,1100,125,150,200,135},
            {0,100,200,280,380,450,450,290,200,80,0}
            };
      int [][]YArrays={{300,325,216,226,335,310},
            {100,275,375,550,550,375,275,125,575,540,530,565,200,150,525,525,100,100,140,190,110},
            {550,575,560,560,575,575,550},
            {550,575,575,500,475,450,470,520,600,600,570}
      };

public void init()
{
      setSize(450,700);
      setBackground(Color.WHITE);
}
public void paint (Graphics myGraphics)
{
      int i=0;
      int j=0;

      for(int count=0;count<3,count++)
      {
      myGraphics.fillPolygon(XArrays[i],YArrays[j],Xarrays.length);
      //Last component gets the length of the array, not sub-array
      
      if (count==0)
      {
      myGraphics.setColor(Color.YELLOW);
      i=0, j=0;
      }
      else if (count==1)
      {
      myGraphics.setColor(Color.ORANGE);
      i=0, j=0;
      }
      else if (count==2)
      {
      myGraphics.setColor(Color.GREEN);
      i=0, j=0;
      }
      else if (count==3)
      {
      myGraphics.setColor(Color.BLUE);
      i=0, j=0;
      }
      }
//Some drawString statements here (not relevant & working OK)
}

      public void  mouseClicked(MouseEvent event){}
      public void mousePressed(MouseEvent event){}
      public void mouseReleased(MouseEvent event){}
      public void mouseExited(MouseEvent event){}
}
0
 
mark_667Author Commented:
Has anyone tried the code I posted? This post has been going on a while now, and I would like to tie it up. The reason I haven't accepted Webstorms last comment is because the XArray.length party will only get the length of the array and not the subarray it is currently in, I have tried it and the polygons look nothing like how I need them (they're supposed to look like roads). The (bad) code that shows what they are meant to look like is below (so you know what I'm trying to achieve) but I started this post as a way of improving on it. I'd still appreciate any feedback.

import element.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
import java.applet.Applet;


public class Asn2Program1 extends Applet implements MouseListener

{

        Font myFont=new Font("Times", Font.BOLD,16);


        int YellowXCoords[]={  0,200,450,450,200,  0};

        int YellowYCoords[]={300,325,216,226,335,310};

       

        int OrangeXCoords[]={200,100,275,125,135, 85,120,110,100,300,400,400,310,230,325,400,410,375,365,325,230,210};

        int OrangeYCoords[]={100,275,375,550,550,375,275,275,125,575,540,530,565,200,150,525,525,100,100,140,190,110};

      

        int GreenXCoords[]={125,100,100,125,150,200,135};

        int GreenYCoords[]={550,575,560,575,575,550,560};

       

        int BlueXCoords[]={  0,100,200,280,380,450,450,480,290,200, 80,  0};

        int BlueYCoords[]={550,575,575,500,475,450,470,500,520,600,600,570};

       

public void init ()

{

    setSize(500,700);

    setBackground( Color.WHITE);

}



public void Draw_Roads (Graphics myGraphics )

{

    myGraphics.setColor(Color.YELLOW);

    myGraphics.fillPolygon(YellowXCoords,YellowYCoords,YellowXCoords.length);



    myGraphics.setColor(Color.ORANGE); //Make into 1 poly

    myGraphics.fillPolygon(OrangeXCoords,OrangeYCoords,OrangeXCoords.length);



    myGraphics.setColor(Color.GREEN);

    myGraphics.fillPolygon(GreenXCoords,GreenYCoords,GreenXCoords.length);



    myGraphics.setColor(Color.BLUE);

    myGraphics.fillPolygon(BlueXCoords,BlueYCoords,BlueXCoords.length);

}



public void paint ( Graphics myGraphics)

{

Draw_Roads(myGraphics);



//Draw Labels

myGraphics.setColor(Color.BLACK);

myGraphics.setFont( myFont);



myGraphics.drawString("Whitworth St West",135,25);

myGraphics.drawString("Lower Mosley St",50,35);

myGraphics.drawString("Albion St",20,40);

myGraphics.drawString("Medlock St",15,75);

myGraphics.drawString("Oxford Rd",115,55);

myGraphics.drawString("Portland St",95,15);

myGraphics.drawString("Brook Street",170,100);

myGraphics.drawString("A57",135,80);



myGraphics.drawString("A",85,15); //Draw points A-G where route will pass through

myGraphics.drawString("B",110,0);

myGraphics.drawString("C",125,20);

myGraphics.drawString("D",145,75);

myGraphics.drawString("E",110,160);

myGraphics.drawString("F",65,20);

myGraphics.drawString("G",20,25);

}

    public void mouseClicked(MouseEvent event){}

    public void mousePressed(MouseEvent event) {}

    public void mouseReleased(MouseEvent event) {}

    public void mouseEntered(MouseEvent event) {}

    public void mouseExited(MouseEvent event) {}

}
0
 
CEHJCommented:
import element.*;
import java.awt.Color;
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;

public class Prog_With_Irritating_Array_Problem extends Applet implements
            MouseListener {

      Font myFont = new Font("Times", Font.BOLD, 16);

      int[][] XArrays = {
                  { 0, 200, 450, 450, 200, 0 },
                  { 200, 100, 75, 125, 135, 110, 200, 300, 400, 400, 310, 230, 325, 400,
                              410, 325, 315, 320, 230, 210 },
                  { 125, 100, 1100, 125, 150, 200, 135 },
                  { 0, 100, 200, 280, 380, 450, 450, 290, 200, 80, 0 } };

      int[][] YArrays = {
                  { 300, 325, 216, 226, 335, 310 },
                  { 100, 275, 375, 550, 550, 375, 275, 125, 575, 540, 530, 565, 200, 150,
                              525, 525, 100, 100, 140, 190, 110 },
                  { 550, 575, 560, 560, 575, 575, 550 },
                  { 550, 575, 575, 500, 475, 450, 470, 520, 600, 600, 570 } };

      Color[] colors = { Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLUE };

      public void init() {
            setSize(450, 700);
            setBackground(Color.WHITE);
      }

      public void paint(Graphics myGraphics) {

            for (int count = 0; count < XArrays.length; count++) {
                  myGraphics.setColor(colors[count]);
                  myGraphics.fillPolygon(XArrays[count], YArrays[count], XArrays.length);
                  // Last component gets the length of the array, not sub-array
            }
            // Some drawString statements here (not relevant & working OK)
      }

      public void mouseClicked(MouseEvent event) {
      }

      public void mousePressed(MouseEvent event) {
      }

      public void mouseReleased(MouseEvent event) {
      }

      public void mouseExited(MouseEvent event) {
      }

      public void mouseEntered(MouseEvent event) {
      }
}
0
 
mark_667Author Commented:
GEHJ I kept getting a redundancy error on the class declaration when I ran your code it also didn't like the line:
import java.applet.Applet;
for some reason is thi to do with the code or IDE settings?
0
 
CEHJCommented:
>> GEHJ I kept getting a redundancy error on the class declaration

Not sure what you mean there

>>it also didn't like the line:
import java.applet.Applet;

I copied the imports from *your* code ;-)

0
 
mark_667Author Commented:
CEHJ: I got your code to run, (the import you mentioned wasn't in my original code, I don't think it was necessary as I had set netbeans to run it as an applet), but it still only draws the first four points of the polygons as Webstorm's did, if you look at, for example, the ornge polygon, it is more complex than that. Although you have made the colour changes a bit more efficient by putting them in an array also. The only part that's really changed is:

Color[] colors = { Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLUE };
and
myGraphics.setColor(colors[count]);
included in the for loop.
0
 
CEHJCommented:
Why? I solved the problem the questioner was asking about
0
 
CEHJCommented:
The reason your polygons are not displaying well is because the coordinates are not right. If you want something looking like a road then you'll need to do something more like this:


import java.awt.Color;
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;

public class Prog_With_Irritating_Array_Problem extends Applet implements
            MouseListener {

      Font myFont = new Font("Times", Font.BOLD, 16);

      int[][] XArrays = {
                  //{ 0, 200, 450, 450, 200, 0 },
                  { 0, 0, 200, 250, 250, 200},
                  { 200, 100, 75, 125, 135, 110, 200, 300, 400, 400, 310, 230, 325, 400,
                              410, 325, 315, 320, 230, 210 },
                  { 125, 100, 1100, 125, 150, 200, 135 },
                  { 0, 100, 200, 280, 380, 450, 450, 290, 200, 80, 0 } };

      int[][] YArrays = {
                  //{ 300, 325, 216, 226, 335, 310 },
                  { 300, 325, 325, 300, 275, 300},
                  { 100, 275, 375, 550, 550, 375, 275, 125, 575, 540, 530, 565, 200, 150,
                              525, 525, 100, 100, 140, 190, 110 },
                  { 550, 575, 560, 560, 575, 575, 550 },
                  { 550, 575, 575, 500, 475, 450, 470, 520, 600, 600, 570 } };

      Color[] colors = { Color.YELLOW, Color.ORANGE, Color.GREEN, Color.BLUE };

      public void init() {
            setSize(450, 700);
            setBackground(Color.WHITE);
      }

      /* (non-Javadoc)
       * @see java.awt.Component#paint(java.awt.Graphics)
       */
      public void paint(Graphics myGraphics) {

            //for (int count = 0; count < XArrays.length; count++) {
            int limit = 1;
            for (int count = 0; count < limit; count++) {
                  myGraphics.setColor(colors[count]);
                  //myGraphics.drawPolygon(XArrays[count], YArrays[count], XArrays.length);
                  myGraphics.fillPolygon(XArrays[count], YArrays[count], XArrays[count].length);
                  // Last component gets the length of the array, not sub-array
            }
            // Some drawString statements here (not relevant & working OK)
      }

      public void mouseClicked(MouseEvent event) {
      }

      public void mousePressed(MouseEvent event) {
      }

      public void mouseReleased(MouseEvent event) {
      }

      public void mouseExited(MouseEvent event) {
      }

      public void mouseEntered(MouseEvent event) {
      }
}
0
 
mark_667Author Commented:
CEHJ the co-ords from my second posting with the bad code definately had the correct co-ords which, I admit had been revised since my original posting. The last code you posted seemed to work with the revised co-ords, which I don't think I tried before, so the points are yours. Some previous postings meant the polygons were drawn without all the points which is why I asked for the mod's advice.
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now