# randomNumber cannot be resolved to a variable - 2d Array

Page 1 of 1

## 1 Replies - 397 Views - Last Post: 07 August 2012 - 08:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=288258&amp;s=da1e1389538872c3bf0f5d2b75b8bdc4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TechSyndrome

Reputation: 3
• Posts: 135
• Joined: 06-May 12

# randomNumber cannot be resolved to a variable - 2d Array

Posted 07 August 2012 - 08:04 PM

Hi. I've been given a task of creating a two-dimensional array that generates 3 integers: 0, 1 or 2. Through some searching on the internet, I've managed to put bits and pieces together (not the best way to go about things), but to no avail. I can print 19x19 integers out in the rows and columns, but I only get 0's. I know how to get random numbers without an array between a set of numbers with the Math.random method, but I can't apply the same to the array.

```import java.util.*;
public class RandomArray
{
public static void main(String[] args)
public class RandomArray
{
public static void main(String[] args)
{
final int ROWS = 19;
final int COLS = 19;

int[][] a2 = new int[19][19];

//... Print array in rectangular form
for (int i =0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {

int randomNumber = (int) (Math.random()*(2-0) + 1);
System.out.print(" " + a2[i][j]);
}
System.out.println(randomNumber);
}

}
}

}
```

I'm fairly new to programming/Java, so please forgive me for anything stupid I've done. Any help would be greatly appreciated.

Is This A Good Question/Topic? 0

## Replies To: randomNumber cannot be resolved to a variable - 2d Array

### #2 Sheph

Reputation: 428
• Posts: 998
• Joined: 12-October 11

## Re: randomNumber cannot be resolved to a variable - 2d Array

Posted 07 August 2012 - 08:44 PM

You are not assigning the variables in your array to anything. You get a random number and save it to the variable randomNumber, but then you don't change your array. After you get randomNumber, try:

a2[i][j] = randomNumber;

Edit: As a side lesson, what does this tell you about the default values for integers (When you don't explicitly assign them)?

This post has been edited by Sheph: 07 August 2012 - 08:50 PM