0

I am trying to make a program that generates 100 integers from 0 to 25 and store them in 3 arrays: one for all the numbers, one for even, one for odd. I am having trouble with adding the variable n to my arrays. My .get is also not working well. Thanks!

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class randomdemo { 
    public static ArrayList randommethod()
    {
        int i = 0;

        ArrayList myArray = new ArrayList();
        ArrayList evenArray = new ArrayList();
        ArrayList oddArray = new ArrayList();

        while(i<=99)
        {
            Random rand = new Random();
            int n = rand.nextInt(25) + 0;

            myArray.add(n);

            if(myArray.get(i) % 1 == 0)
            {
                evenArray.add(n);
            }
            else
            {
                oddArray.add(n);
            }
            i++;
        }
        return myArray;
        return evenArray;
        return oddArray;
    }

    public static void main(String args[])
    {
        randommethod();
    }
}
7
  • 2
    How do you think return works? Commented Dec 12, 2014 at 16:16
  • 1
    Think about what if(myArray.get(i) % 1 == 0) does mathematically. There are also other problems but start here. Commented Dec 12, 2014 at 16:17
  • Kon, thanks and I'll change it to 2. But that was not really my question... Commented Dec 12, 2014 at 16:19
  • As @SotiriosDelimanolis mentioned, you need to read up on how return statements work (hint: you can't return more than once object from a method) Commented Dec 12, 2014 at 16:21
  • Also, stackoverflow.com/questions/15117132/… Commented Dec 12, 2014 at 16:21

2 Answers 2

3

You can't have three returns from a single method, and you don't use the result anyway. Please don't use raw-types. And, you could make those List(s) static so you can use them after calling your method. You should declare your Random before starting your loop. Finally, even is % 2 == 0. Something like,

static List<Integer> myArray = new ArrayList<>();
static List<Integer> evenArray = new ArrayList<>();
static List<Integer> oddArray = new ArrayList<>();

public static void randommethod() {
    Random rand = new Random();
    for (int i = 0; i < 100; i++) {
        int n = rand.nextInt(26); // <-- n + 0 is n. 0 to 25.
        myArray.add(n);
        if (n % 2 == 0) { // <-- test n, no need to get it again.
            evenArray.add(n);
        } else {
            oddArray.add(n);
        }
    }
}

and then

public static void main(String args[]) {
    randommethod();
    System.out.println("All: " + myArray);
    System.out.println("Even: " + evenArray);
    System.out.println("Odd: " + oddArray);
}
Sign up to request clarification or add additional context in comments.

12 Comments

Doing this way, at most you get 50 odds and 50 evens
@user3437460 That is not true.
You can simplify the code to (n%2==0? even: odd).add(n); but that’s a matter of coding style…
@ElliottFrisch Hmm I am not sure what exactly OP wants, does he wants 100 evens, 100 odds, 100 all numbers?
@user3437460: you should understand randomness; of course, it is perfectly possible to get 100 odd and 0 even numbers or vice versa. What the OP wants is clearly visible when you look at the code example contained it the question.
|
0

There are a few ways to do this.

Random rnd = new Random();
ArrayList<Integer> all = new ArrayList<Integer>();
ArrayList<Integer> even = new ArrayList<Integer>();

//Add 100 integers of all numbers
for(int x=0; x<100; x++)
    all.add(rnd.nextInt(26)) //26 gives random 0-25

//Add 100 integers of even numbers
int x=0;
while(x<100){
    int rand = rnd.nextInt();
    if(rand % 2 == 0){
       even.add(rand);
       x++
    }     
}

Generating odd numbers are same as the way you generate even numbers. Just change the condition to if (rand % 2 != 0)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.