Reputation: 1
I've been trying for some hours now to get my class to work correctly. I'm trying to have a multidimensional array filled with random numbers created by my 'generateSample' method, then print the array.
public class GenerateUranium
{
int tray [][] = new int [5][3];
private int grading = 0;
private Random randomGenerator;
public GenerateUranium()
{
randomGenerator = new Random();
}
public void generateSampleUranium()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 3; j++)
{
grading = randomGenerator.nextInt(50);
printTray(tray);
}
}
}
public void printTray(int a[][])
{
for( int row = 0 ; row < a.length ; row++) {
for (int column = 0 ; column< a[row].length ; column++) {
System.out.print(a[row][column]+"\t");
}
}
}
}
I've tried following many examples on the Internet, but they're always for printing a specified array, not one being randomly generated by another method. So the trouble I'm having is linking the numbers created in the 'generateSample' to the array itself, so that I may successfully print them like so;
4 3 6
1 6 8
4 7 9
But as I have not been successful in linking my array between my methods for both adding to it and printing it, it is blank.
It's of last report I post for help, but any help would be greatly appreciated. Thanks.
Upvotes: 0
Views: 114
Reputation: 30285
Your generateSample
method needs to actually write values into the array. Then, only after the entire 2D array is full, you should print it:
public void generateSample() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
this.tray[i][j] = randomGenerator.nextInt(50);
}
}
printTray(tray);
}
Upvotes: 4