I have pasted my code below. The array does give me the random numbers, however at the end of the array the errors pop up and I have been unable to fix the errors. The goal is to produce an array with 2 columns and n amount of rows. randomized ints will fill in the array. I have a main in which I've created a 2-d array to hold the values, and I am using the Array.fill() method to call on a method I made randArray that makes the actual array with randomized ints.
The problem is once the code has run, it gives me a few errors that I haven't been able to get rid of. These are the errors:
Exception in thread "main" java.lang.ArrayStoreException: [[I
at java.base/java.util.Arrays.fill(Arrays.java:3639)
at Animal.main(Animal.java:22)
Things I have tried:
- change the range to range-1
- changing the rows to rows-1
- changing the i and j values inside the second method
- throwing my textbook into the garbage where it belongs
What direction should I be going in? Any help would be much appreciated.
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random; //import library for generating random numbers
//Creating an array with 2 columns, the number of rows are based on user input and the numbers
//inside the array are randomized between 0 and x with user giving x
public class Animal {
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int numNodes = input.nextInt(); //this will be the number of rows in the array
System.out.println("Enter the maximum: ");
double max = input.nextDouble(); //array data is inside this range
int[][] something = new int [numNodes][];
Arrays.fill(something, randArray(numNodes,max));
// System.out.println("The rows are: " + arrayMade(numNodes,networkSize)); //testing the array to see if it works
System.out.println(something);
}
/** Creating an array to fill the random array with random ints
*
* @param numRows the number of rows the array will have
* @param range the maximum for a value in the array
* @return randArray returns a randomized filled in 2-d array
*/
public static int[][] randArray (int numRows, double range) //method to make the array
{
int[][] randArray = new int[numRows+1][2];
Random rand = new Random();
int count = numRows-1;
int i = 0;
while(i < count){ //this loops based on how many rows there are
for (int j = 0; j < 1; j++)
{
randArray[i][0] = rand.nextInt((int)range) ; // random integer from 0 to max
randArray[i][1] = rand.nextInt((int)range) ;
System.out.println(randArray[i][0] + " " + randArray[i][1]); //test line
i++;
}//end for-loop
}//end while-loop
return randArray;
}//end arrayMade
}//end class
You don't have to use Arrays.fill
, just assign the return value of your randArray
function to the 2d array.
int[][] something = randArray(numNodes, max);
PS: you need a way to print the 2d array to the console, using System.out.println(something)
will output unexpected result.
Also, remove the for (int j = 0; j < 1; j++)
loop, it's really unnecessary.
Here's the final code you can refer to:
import java.util.Random;
import java.util.Scanner;
//Creating an array with 2 columns, the number of rows are based on user input and the numbers
//inside the array are randomized between 0 and x with user giving x
public class Animal {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows: ");
int numNodes = input.nextInt(); //this will be the number of rows in the array
System.out.println("Enter the maximum: ");
int max = input.nextInt(); //array data is inside this range
int[][] something = randArray(numNodes, max);
// System.out.println("The rows are: " + arrayMade(numNodes,networkSize)); //testing the array to see if it works
System.out.println(something);
}
/**
* Creating an array to fill the random array with random ints
*
* @param numRows the number of rows the array will have
* @param range the maximum for a value in the array
* @return randArray returns a randomized filled in 2-d array
*/
public static int[][] randArray(int numRows, int range) //method to make the array
{
int[][] randArray = new int[numRows + 1][2];
Random rand = new Random();
int count = numRows - 1;
int i = 0;
while (i < count) { //this loops based on how many rows there are
randArray[i][0] = rand.nextInt(range); // random integer from 0 to max
randArray[i][1] = rand.nextInt(range);
System.out.println(randArray[i][0] + " " + randArray[i][1]); //test line
i++;
}//end while-loop
return randArray;
}//end arrayMade
}//end class