Megan Louise Downey
Megan Louise Downey

Reputation: 37

java arrays random scanner class

I am stuck in my beginner java course and am trying to get my array to print out with the user's input on randomly selected index's. With the code I have so far it will only print out an index with all 0's such as this "{0, 0, 0, 0, 0, 0}"

Here is the prompt: Create an empty int array of size 6.

Create a method called populateArray that will pass an array, use random class to choose a random index and prompt the user to enter a value, store the value in the array. Note: Generate 6 random index numbers in an attempt to fill the array.

Create a method called printArray that prints the contents of the array using the for each enhanced loop.

Here is my code:

public class ChangeUp {

    public static void main(String[] args) {
        int[] array = new int[6];
        System.out.println("Please enter 6 numbers to add to a list.");
        populateArray(array);
        printArray(array);
    }

    public static void populateArray(int[] array) {
        Random r = new Random();
        Scanner input = new Scanner(System.in);
        int rArray = r.nextInt(array.length);
        int i = 0;
        for (i = rArray; i <= array.length; i++) {
            i = input.nextInt();
        }
    }

    public static void printArray(int[] array) {
        System.out.print("{" + array[0]);
        for (int i = 1; i < array.length; i++) {
            System.out.print(", " + array[i]);
        }
        System.out.println("}");
    }
}

Upvotes: 0

Views: 681

Answers (3)

Stultuske
Stultuske

Reputation: 9437

for (int i = rArray; i <= array.length; i++) {
    i = input.nextInt();
}

Here is your problem. You assign the value(s) to i, not to elements of the array.

Turn:

i = input.nextInt();

into:

array[i] = input.nextInt();

Upvotes: 2

Oleg Cherednik
Oleg Cherednik

Reputation: 18255

You did not assigne input values to the array: array[i] = ....

I would recommend you to not randomly insert an elements, but just fill an array and then shuffle it.

public class ChangeUp {

    public static void main(String... args) {
        int[] arr = new int[6];
        System.out.format("Please enter %d numbers to add to a list:\n", arr.length);
        populateArray(arr);
        printArray(arr);
    }

    public static void populateArray(int... arr) {
        Scanner scan = new Scanner(System.in);
        List<Integer> tmp = new ArrayList<>(arr.length);

        for (int i = 0; i < arr.length; i++)
            tmp.add(scan.nextInt());

        Collections.shuffle(tmp);
        int i = 0;

        for (int val : tmp)
            arr[i++] = val;
    }

    public static void printArray(int... arr) {
        System.out.println(Arrays.stream(arr)
                                 .mapToObj(String::valueOf)
                                 .collect(Collectors.joining(", ", "{", "}")));
    }
}

Demo:

Please enter 6 numbers to add to a list:
1
2
3
4
5
6
{4, 3, 1, 5, 2, 6}

Upvotes: 0

elehtine
elehtine

Reputation: 108

You do not change any values of array. You can do it with array[i] = value where i is index which value you want to change. Here is example of what populateArray would looks like:

public static void populateArray(int[] array) {
    Random r = new Random();
    Scanner input = new Scanner(System.in);
    int i = r.nextInt(array.length);
    array[i] = input.nextInt();
}

Upvotes: 0

Related Questions