# MAYDAY MAYDAY / \< ArrayIndexOutOfBoundsException: 3\>

**URL:** <https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 28, 2021, 12:57pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702 "2021-04-28T12:57:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [April 28, 2021, 12:57pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/1 "2021-04-28T12:57:43Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

```auto
int amount = 3;
PImage img1;
int[][] state = new int[amount][amount];
IntList n;
int z;
int b =int(sq(amount)-1);
int x, y;
int tileW = width/amount;
int tileH = height/amount;
int dx, dy;

/void setup() {

  size(900, 900);
  img1 = loadImage("me.jpg");
  img1.resize(width, height);

  n = new IntList();
  for ( int d=1; d<10; d++) {
    n.append(d);
    //println(n);
  }

  for ( x = 0; x< state.length; x++) {
    for ( y = 0; y <state[x].length; y++) {

      b--;
      z =int( random(b));

      state[x][y] = n.get(z);
      n.remove(z);n.append(z);
      //println(state[x][y]);
      // println(n);
    }
  }
}

void diff() {
  for (int num =0; num<sq(amount); num++) {
    for (int num2 =0; num2< amount; num2++) {
      for (int num3 =0; num3< amount; num3++) {
        if (state[x][y]==num) {
           dx = int(num2*tileW);
           dy = int(num3*tileH);
        }
      }
    }
  }
}

void draw() {
  background(0);
  //imageMode(CORNER);

  for (int x = 0; x< state.length; x++) {
    for (int y = 0; y <state[x].length; y++) {
      int sh = int(tileW);
      int sw = int(tileH);
      int sx = int(tileW*x);
      int sy = int(tileH*y);
      
      diff();

      int dh = sw;
      int dw = sh;
      copy(img1, sx, sy, sw, sh, dx, dy, dw, dh);
    }
  }
}

```

//I learn this for a shot time . i want make a picture with 9 part form the same sourse picture . they are in different and specfic place randomly.But now i got \< ArrayIndexOutOfBoundsException: 3\>  
here if (state[y]==num) {  
at 46 lines

**strong text**

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [April 28, 2021, 2:32pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/2 "2021-04-28T14:32:00Z")

</div>

Hi,

Welcome to the forum! 😉

Just before we can help you, can you please format your code by using the `</>` button or put backticks around your code like this : ```code here``` → `code here`

Also you can press `Ctrl+T` in the Processing IDE to auto indent your code.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 28, 2021, 4:33pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/3 "2021-04-28T16:33:35Z")

</div>

do you mean the line in the function diff()?

x,y is not known there, maybe use num2 and num3?

Hey, Welcome to the forum!

Chrisir

here is your code better formatted (code not working really)

```auto

int amount = 3;
PImage img1;
int[][] state = new int[amount][amount];
IntList n;
int z;
int b = int(sq(amount)-1);
int x, y;
int tileW;
int tileH;
int dx, dy;

void setup() {
  size(900, 900);

  tileW = width/amount;
  tileH = height/amount;

  img1 = loadImage("me.jpg");
  img1.resize(width, height);

  n = new IntList();
  for ( int d=1; d<10; d++) {
    n.append(d);
    //println(n);
  }

  for ( x = 0; x< state.length; x++) {
    for ( y = 0; y <state[x].length; y++) {

      b--;
      z =int( random(b));

      state[x][y] = n.get(z);
      n.remove(z);
      n.append(z);
      //println(state[x][y]);
      // println(n);
    }
  }
}

void draw() {
  background(0);
  //imageMode(CORNER);

  for (int x = 0; x< state.length; x++) {
    for (int y = 0; y <state[x].length; y++) {
      int sh = int(tileW);
      int sw = int(tileH);
      int sx = int(tileW*x);
      int sy = int(tileH*y);

      diff();

      int dh = sw;
      int dw = sh;
      copy(img1, sx, sy, sw, sh, dx, dy, dw, dh);
    }
  }
}

void diff() {
  for (int num =0; num<sq(amount); num++) {
    for (int num2 =0; num2< amount; num2++) {
      for (int num3 =0; num3< amount; num3++) {
        if (state[num2][num3]==num) {
          dx = int(num2*tileW);
          dy = int(num3*tileH);
        }
      }
    }
  }
}
//

```

---

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [April 29, 2021, 5:41am UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/4 "2021-04-29T05:41:54Z")

</div>

OK i think i understand what you mean,My English is kind of bad…

---

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [April 29, 2021, 5:48am UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/5 "2021-04-29T05:48:58Z")

</div>

thanks for helping me , i see the problem,i will think about it for a while

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 29, 2021, 3:20pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/6 "2021-04-29T15:20:12Z")

</div>

> [@zang](#):
>
> OK i think i understand what you mean,My English is kind of bad…

Most browsers allow you to translate languages.

@josephh was politely asking you to format your code.

I am asking also…

Please format the code in your original post:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

You can use the edit button (looks like a pencil):  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/ea66ff565e030b0508baa76d9d76eeab272ae0d4.png)

`:)`

---

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [April 30, 2021, 1:14pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/7 "2021-04-30T13:14:10Z")

</div>

i tried everyting you told me but nothing happened

> [@zang](#):
>
> … \</\> ‘’’///

> [@zang](#):
>
> format code with \</\> button

> [@josephh](#):
>
> `code here`

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 30, 2021, 1:21pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/8 "2021-04-30T13:21:17Z")

</div>

nothing is impossible

[https://www.khm.de/~n.hieda/discourse-code.mp4](https://www.khm.de/~n.hieda/discourse-code.mp4)

---

<div class="post-metadata">

**Author:** ![zang](https://avatars.discourse-cdn.com/v4/letter/z/bc8723/32.png) [@zang](https://discourse.processing.org/u/zang)\
**Post date:** [April 30, 2021, 1:35pm UTC](https://discourse.processing.org/t/mayday-mayday-arrayindexoutofboundsexception-3/29702/9 "2021-04-30T13:35:56Z")

</div>

ok done! thats much easier to understand. 🤣
