user1729377
user1729377

Reputation: 89

Java Random Arrays

Im working on something for class, and i'm stuck. I believe im pretty close, but dont know where to go. When I run my code im getting my arrays ten times, which must have something to do with my loop. Also, in my arrays, im not sure how to create the range from 20-50. Currently mine is from 1-50. Lastly, my output is not exactly what is needed. Any help would be greatly appreciated. Here is the question, followed by my code so far.

Create an array named array1 with 10 random integer numbers in the range of [20 50]. Then create an array named array2 with the same size of array1. Then copy the numbers in array1 that are greater than 35 to array2. Pad 0s in array2 if not enough numbers copied to it. For example, if array1 is {34, 23 45, 39, 28, 41, 33, 23, 42, 48}, array2 will be{45, 39, 41, 42, 48, 0,0,0,0,0}

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;

public class Arraylab6 {
    public static void main(String[] args) {
        int x;
        int[] array1 = new int[10];
        int[] array2 = new int[10];
        Random rand = new Random();
        for (int i = 0; i < array1.length; i++) {
            int h = rand.nextInt(50);
            array1[i] = h;
        }
        System.out.println(Arrays.toString(array1));
        for (int i = 0; i < array1.length; i++) {
            if (array1[i] > 35) {
                array2[i] = array1[i];
            } else {
                array2[i] = 0;
            }
            System.out.println(Arrays.toString(array1));
            System.out.println(Arrays.toString(array2));
        }
    }
}

Upvotes: 3

Views: 200

Answers (6)

Nik.exe
Nik.exe

Reputation: 411

Previous answers are correct about Random and println() method. I just want to add fancy way how to populate array2 from array1 in java8 :) Instead of:

for (int i = 0; i < array1.length; i++) {
        if (array1[i] > 35) {
            array2[i] = array1[i];
        } else {
            array2[i] = 0;
        }
}

you can use this java 8 code:

   int[] filtered = Arrays.stream(array1) //make a stream of array1
        .filter(value -> value > 35) //if value is greater than 35, leave that value
        .toArray(); //create array from the result
    //create array with zeroes that need to be added
    int[] zeroes = new int[array1.length - filtered.length];
    Arrays.fill(zeroes, 0);
    //concat zeroes
    array2 = ArrayUtils.addAll(filtered, zeroes); //you have your final array :)

Upvotes: 1

user6904265
user6904265

Reputation: 1938

Another pretty implementation:

int[] array1 = new Random().ints(10, 20, 51).toArray();
int[] temp = Arrays.stream(array1).filter(x -> x > 35).toArray();
int[] array2 = new int[10];
System.arraycopy( temp, 0, array2, 0, temp.length );
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));

Upvotes: 1

duncan
duncan

Reputation: 1161

Calculate a random number between the range you want then add the starting value:

int h = rand.nextInt(31) + 20;

Also for the printing multiple times you have the print inside the for loop. You should move the lines:

System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));

outside of the for loop.

Upvotes: 3

MaxZoom
MaxZoom

Reputation: 7743

You can use ThreadLocalRandom class to generate random number in a given scope:

ThreadLocalRandom.current().nextInt( min, max+1);

Upvotes: 2

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

You have a couple issues. First, your random generator should look like this:

int random = (int) (Math.random() * 31 + 20);

Secondly, you need to pull your output statements out of your last for loop:

   for (int i = 0; i< array1.length; i++)
   {
      if (array1[i]>35)
      {
        array2[i]=array1[i];
      }
      else {
         array2[i]=0;
      }


   }
   System.out.println(Arrays.toString(array1));
   System.out.println(Arrays.toString(array2));
 }
}

Upvotes: 2

Zircon
Zircon

Reputation: 4707

im not sure how to create the range from 20-50. Currently mine is from 1-50.

int h = rand.nextInt(31)+20; //nextInt(31) = range from 0-30, then add 20 to the result

When I run my code im getting my arrays ten times

Your println calls are in your for loop, move them down.

Also, your solution does not satisfy the requirements, because you're putting any number >35 into the same index as the other array. Instead, keep track of your insert index, and move it when you copy the int:

int copyIndex = 0;
for (int i = 0; i< array1.length; i++)
{
  if (array1[i]>35){
    array2[copyIndex++]=array1[i]; //Add 1 to copyIndex each time a value is copied
}

You also don't need to "pad" with zeroes because int's default value is already 0.

Upvotes: 5

Related Questions