2

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();
    }
}
1
  • 1
    You never ask the program to print anything (such as using a method like println). It's therefore expected that nothing is printed out. Commented Dec 11, 2014 at 17:51

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I have another quick question about the program. See the edited question. Thanks!
Java arrays are not dynamically sized data structures. You can't do that. You could size them both the same as the input (and leave empty slots). Or, you could use a Collection like an ArrayList<Integer> which is dynamic.
1

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.

2 Comments

Can you explain some more?
@YoungDeezie I have edited the answer to include further details.
0

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.

Comments

0

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();
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.