Reputation: 145
Revised question:
I want the even elements of my array to be stored in a corresponding array. My if else statements do that. Since there will always be a varying number of evens and odds each run, I want the size of the evenArray
and oddArray
to adjust with each iteration of my while loop. I get an error when compiling that says I'm not doing that part right.
import java.util.Arrays;
import java.util.Random;
public class randomdemo {
public static int[] randommethod()
{
int i = 0;
int[] myArray;
myArray = new int[100];
int[] evenArray;
int[] oddArray;
while(i<=99)
{
Random rand = new Random();
int n = rand.nextInt(25) + 0;
myArray[i] = n;
if(myArray[i] % 2 == 0)
{
evenArray = new int[i];
evenArray[i] = n;
}
else
{
oddArray = new int[i];
oddArray[i] = n;
}
i++;
}
return myArray;
}
public static void main(String args[])
{
int[] result = randommethod();
System.out.println(Arrays.toString(result));
randommethod();
}
}
Upvotes: 2
Views: 74
Reputation: 1323
If you want a random int between 0 and 25 inclusive, then your code should be:
int n = rand.nextInt(26); //I want a random int between 0 and 25 inclusive
Upvotes: 0
Reputation: 17454
Other than the things mentioned by other user, if I were you, I will write your method this way:
public static int[] randommethod() //declaring method
{
Random rnd = new Random(); //using the random class
int[] myArray = new int[100]; //create and initializing array in 1 line
for(int x=0; x<myArray.length; x++) //Normally use for-loop when you know how many times to iterate
myArray[x] = rnd.nextInt(26); //0-25 has 26 possibilities, so just write 26 here
return myArray; //returns the array
}
It will do exactly the same thing, I am editing this from your original codes.
In the main..
public static void main (String[] args)
{
int[] myArray = randommethod();
}
Upvotes: 0
Reputation: 17454
If you just want to print out the array without storing, write this in the main. 100% it will work.
System.out.println(Arrays.toString(randommethod())); //print array
At this line, you returned the vaule:
return myArray; //returns the array
But you did not store it anywhere. So the return value is lost. Even though you did all the work in the method.
Store your return array as follows in the main
int[] myArray = randommethod(); //store returned value (from method)
After that, you can do anything you want with the returned array.
Upvotes: 0
Reputation: 9403
The returned array is not being used.
So the return from randommethod()
is an int[]
but the main method does not print it (or use it in any way).
Here is one way to use it:
int[] outputRandomAry = randommethod();
for (int elem : outputRandomAry) {
System.out.print(elem + ", ");
}
System.out.println();
Also you might want to put the Random rand = new Random(); //using the random class
outside the while loop. This prevents unnecessary spinning off new objects for each rand.
And you can use int n = rand.nextInt(26);
for 0(inclusive) to 26(exclusive) gives you the desired range.
Upvotes: 1
Reputation: 201439
Store the result, and maybe print it. Your could use a loop or Arrays.toString(int[])
. Something like,
int[] result = randommethod();
System.out.println(Arrays.toString(result));
When I put both lines in main()
and use your posted randommethod()
it appears to work.
Upvotes: 3