1

This is my first time with arrays. I should prompt the user to enter 5 array values and then display them in random order. I am quite confused, since it's my first time doing this. Anyway, my code is here.

import java.util.*;
public class Test {
    public static void main(String[] args) {
        int myArray[] = new int[5];
        System.out.println("Please enter 5 numbers: ");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < myArray.length - 1; i--) {
            int j = (int) (Math.random() * (i + 1));
            myArray[i] = input.nextInt();


            System.out.println("The numbers are: ");

            System.out.println(myArray[0]);
            System.out.println(myArray[1]);
            System.out.println(myArray[2]);
            System.out.println(myArray[3]);
            System.out.println(myArray[4]);


            int temp = myArray[i];
            myArray[i] = myArray[j];
            myArray[j] = temp;

            System.out.println("The numbers, shuffled, are: ");
            System.out.println(myArray[0]);
            System.out.println(myArray[1]);
            System.out.println(myArray[2]);
            System.out.println(myArray[3]);
            System.out.println(myArray[4]);
        }
    }
}

Thank you everyone for your support.

user412308
  • 59
  • 9
  • 1
    Hint: you can insert in random order as well. – Falla Coulibaly Nov 08 '16 at 19:09
  • The program should randomize the input entered by the user, and display the output. – user412308 Nov 08 '16 at 19:09
  • Since you want to display it in both the actual and shuffled order, it may be best to save it in order. The easy way to shuffle it would be to convert it into a list and use the Collections.shuffle method. You could also randomly get the array indexes and print them in a random order (rather than shuffling the array and printing it in order). Be careful not to print the same index twice though – Allan W Nov 08 '16 at 19:11
  • Given that you know the size of your array: 1-you can generate random index (number between 0 and array.length) 2-if there is nothing at that index insert the user input otherwise repeat 1. – Falla Coulibaly Nov 08 '16 at 19:13
  • Nothing in that for loop will run – jthort Nov 08 '16 at 19:13
  • LOL the entire code is broken, he doesn't even get the number -.- Please come back when you're able to actually get user input into the first array. – JordanGS Nov 08 '16 at 19:22

2 Answers2

0

A - Explanation

Let's say you take the input values in order as {'1','2','3','4','5'}. What shuffling is corrupting the order randomly, so you have to change the position of elements randomly.

In the demo code,

  • swapArrayElement swaps the elements those that positions are passed as parameters.

  • getRandom returns a random value between 0 and the range which passed to the method as a parameter.

  • shuffleArray shuffles the array by changing the positions of elements randomly. Please notify that there is an additional boolean isShuffled[] array and it is boolean because we have to keep the track of positions whether they are shuffled or not.

  • isArrayShuffled method, checks that if all positions are shuffled or not.

B - Demo Code

import java.util.Scanner;

public class Test {

    public static final int ARRAY_LENGTH = 5;

    public static void main(String[] args) {
        int myArray[] = new int[ARRAY_LENGTH];



        Scanner input = new Scanner(System.in);
        System.out.println("Please enter 5 numbers: ");

        for(int i = 0; i < myArray.length; i++)
            myArray[i] = input.nextInt();

        System.out.println("\nThe numbers are: ");
        printIntArray(myArray);

        shuffleArray(myArray);
        System.out.println("\nThe numbers, shuffled, are: ");
        printIntArray(myArray);

        input.close();  // no memory leaks!
    }

    // method for printing array
    public static void printIntArray(int[] array) {
        for(int i = 0; i < array.length; i++)
            System.out.printf("%2d ", array[i]);
        System.out.printf("%n");    // use %n for os-agnostic new-line
    }

    // method for shuffling array
    public static void shuffleArray(int[] array) {
        int range = array.length;
        boolean isShuffled[] = new boolean[range];  // store which positions are shuffled

        while(!isArrayShuffled(isShuffled)) {
            int positionSrc = getRandom(range);
            int positionDst = getRandom(range);

            swapArrayElement(array, positionSrc, positionDst);
            isShuffled[positionSrc] = true;
            isShuffled[positionDst] = true;
        }

    }

    public static int getRandom(int maxRange) {
        return (int)(Math.random()*maxRange);
    }

    public static void swapArrayElement(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static boolean isArrayShuffled(boolean[] isShuffled) {
        for(int i = 0; i < isShuffled.length; i++)
            if(isShuffled[i] == false)
                return false;

        return true;
    }
}

C - Demo Output

Please enter 5 numbers: 
1 2 3 4 5

The numbers are: 
 1  2  3  4  5 

The numbers, shuffled, are: 
 4  2  5  1  3 
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
-1
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class Test {

    public static void shuffle(int[] arr) {
        Random rnd = ThreadLocalRandom.current();
        for (int i = arr.length - 1; i > 0; i--) {
            int index = rnd.nextInt(i + 1);
            int t = arr[index];
            arr[index] = arr[i];
            arr[i] = t;
        }
    }

    public static void main(String[] args) {
        int myArray[] = new int[5];
        System.out.println("Please enter 5 numbers: ");
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < myArray.length; i++) {
            System.out.println("Enter " + (i + 1) + ". number: ");
            myArray[i] = input.nextInt();
        }
        System.out.println("The numbers are: ");
        for (int j2 = 0; j2 < myArray.length; j2++) {
            System.out.println(myArray[j2]);
        }

        shuffle(myArray);

        System.out.println("The numbers, shuffled, are: ");
        for (int j2 = 0; j2 < myArray.length; j2++) {
            System.out.println(myArray[j2]);
        }
    }
}
Alican Balik
  • 1,284
  • 1
  • 8
  • 22