# Thread: Two simple array problems.

1. Member
Join Date
Nov 2009
Posts
2
Rep Power
0

## Two simple array problems.

Hi, I'm new to Java and was wondering if anyone could help me with these two array problems.

1:

I have made this code that works to create multiplication tables with for loops:
Java Code:
public class MultiplyTables
{

public MultiplyTables()
{

}

public void printTable(int x, int y)

{
int [][] grid = null;
grid = new int [x+1][y+1];

for (int row = 1; row < grid.length; row++)
{
for (int col = 1; col < grid[row].length; col++)
{
grid[row][col] = row * col;
}
}
for (int row = 1; row < grid.length; row++)
{
for (int col = 1; col < grid[row].length; col++)
{
System.out.print(grid[row][col] + " \t");
}
System.out.println();
}
}
}
I'm trying to recreate this with while loops and have got the following which doesn't work:

Java Code:
public class MultiplyTables
{

public MultiplyTables()
{

}

public void printTable(int x, int y)

{
int [][] grid = null;
grid = new int [x+1][y+1];

int row = 1;

while (row < grid.length)
{
int col = 1;
while (col < grid[row].length)
{
grid[row][col] = row * col;
col++;
}
}

row++;

int row1 = 0;
while (row1 < grid.length)
{
int col1 = 1;
while (col1 < grid[row].length)
{
System.out.print(grid[row1][col1] + " \t");
col1++;
}

row++;

System.out.println();

}
}
}
Any ideas?

2. I have stored cubed numbers in an array and am trying to call up numbers from that array in the class methods but I am getting Null pointer errors even when I specifically ask for a value that should be there.

Java Code:
public class StoredCubes
{
private int j = 0;
int[] cubes;
private int numberOfCubes = 0;

public StoredCubes(int n)
{
numberOfCubes = n;
int[] cubes = new int[numberOfCubes];
for (int i=0; i<cubes.length; i++)
{
cubes[i] = j*j*j;
j = (j+1);
}
System.out.println(cubes[3]);
}

public void printOneCube(int k)
{

System.out.println(cubes[3]);

}

public void printMCubes(int m)
{
int i=0;
while(cubes[i] != m)
{
i++;
}
System.out.println(cubes[i]);
}

}
Any ideas with this? I'm guessing I'm not initialising the array properly and the cubes aren't being stored. The printing of the third value in the constructor is just there to show that it does work initially but the same code later brings up an error.

2. in the first problem, the second while loop, you are testing on row1, but incrementing row

while (row1 < grid.length)
{
....
row++;

3. for second problem, you are setting the number of cubes 0 (never setting it), and later trying to access the 3rd index
private int numberOfCubes = 0;

....
System.out.println(cubes[3]);
when you try to access an array index out of the boundary of the array length, you would get an array index out of bounds exception.

I don't get how you would get a null pointer exception though.

also, for the get cubes, you should test the boundary length of the array. and maybe you meant the system.out.printlnof the cubes[i] to be inside the while loop ?

Java Code:
public void printMCubes(int m)
{
int i=0;
while(i < cubes.length && cubes[i] != m)
{
System.out.println("cube[" + i + "]:" + cubes[i]);
i++;
}
Last edited by travishein; 11-06-2009 at 02:43 AM.

4. Member
Join Date
Nov 2009
Posts
2
Rep Power
0
Hi, Thank you for looking at my code.

For the first problem:
changing row to row1 doesn't change anything, it was just a typo. I think the problem is in how I have set out my while loops as the version with for loops works perfectly.

For the Second:
The problem is that I can't refer to the values in the array that were set in the constructor from a method.

if I change the System.out.println to (cubes[i]) and put it underneath the j=j+1 line then it prints out the cubes fine. The problem is just when I try and get values from the array using a method, even just asking for cubes[2] when there are 10 values stored in it doesn't work.

5. Hi,
One major flaw in ur code...U have initialized globally int[] cubes...again in constructor u have again declated int[] cubes supoose to be a local one restricted within constructor.So,whatever manipulations u are doing will not reach the int [] cubes declalared globally.

Please got thru sun tutorial for basics and the d..

#### Posting Permissions

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