cerulean
cerulean

Reputation: 25

Generating a sequence of random values to print. Sorting and printing again. Java

Hello I'm still a beginner in Java and I needed to write a program that generates a sequence of 20 random values between 0 and 99 in an array, prints the sequence, sorts it, and then prints the sorted sequence. I have the code below it works perfectly for my assignment. It prints the set of random values and then prints the sorted set of values. Do you know if there is any way to simplify the code that I have like in how to print the random values for example? I think there may be a way to simplify the code in its entirety but I'm not too sure. Please let me know if you have some suggestions to make to code look cleaner or easier.

import java.util.Arrays;

public class Array {

    public static void main(String[] args) {
        //This will create an array to hold 20 integers.
        int[] array = new int[20];
        for (int i = 0; i < array.length; i++)
        {
            array[i] = (int) (Math.random() * 99 + 1);
        }
        //This will print the random sequence of values.
        System.out.print("Random sequence of values: ");

        for (Integer i : array) {
            System.out.print(i.intValue() + " ");
        }
        System.out.println("");
        System.out.println("");
        //This will print the sorted sequence of values.
        System.out.print("Sorted sequence of values: ");
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));

    }

}

Upvotes: 1

Views: 194

Answers (2)

kofemann
kofemann

Reputation: 4423

Well, something this:

new Random().ints(20,0,100).forEach(System.out::println);

or if you want to print sorted

new Random().ints(20,0,100).sorted().forEach(System.out::println);

Or, if you want to avoid streams

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

public class Array {

    public static void main(String[] args) {
        Random random = new Random();
        //This will create an array to hold 20 integers.
        int[] array = new int[20];
        for (int i = 0; i < array.length; i++)
        {
            array[i] = random.nextInt(100);
        }
        //This will print the random sequence of values.
        System.out.println("Random sequence of values: " + Arrays.toString(array));

        Arrays.sort(array);
        //This will print the sorted sequence of values.
        System.out.println("Sorted sequence of values: " + Arrays.toString(array));
    }
}

Upvotes: 2

WJS
WJS

Reputation: 40034

To answer your specific question you can use this to generate the random numbers.

Random r = new Random();

Then for each call to random use:

// generates between 0 and 99 inclusive.
array[i] = r.nextInt(100);

To print the values you can do this:

for (int i : array) {
   System.out.print(i + " ");
}

The streams approach is also possible but I would stick with what you have until you get familiar with the basics of Java.

Upvotes: 1

Related Questions