3

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));
        }
    }
}
2
  • Print after the for-loop, not inside it. Commented Nov 10, 2016 at 19:21
  • Think about how you are copying the values in array2. If the first number >35 is at index 5, where should you place it in the array2 ? Where does your code place it right now? Commented Nov 10, 2016 at 19:24

6 Answers 6

5

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.

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

Comments

3

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.

Comments

2

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

2 Comments

Thats almost perfect. I just need to move the zeros to the back of array two now. Any ideas??
Oh, you want to move the zero's to the back? Well that would be a different question. But you'd have to go over the array again and create a new array with all the elements equaling 0, the check the sorted array for a number, if it isn't 0, the add it to new array, otherwise skip. and that will leave you the array you want.
2

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

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

1 Comment

I like this solution, but I don't think the OP can use something like that in homework without some explanation.
1

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

Comments

1

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 :)

1 Comment

nice java8 implementation, but the request is that array2 must have zeros at the end.

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.