1

I am pretty new in this world, and I must say sometimes things that looks easy are pretty harsh.

I am stuck with a task that entails dealing with an array and for-loops.

I should iterate over the array and for every iteration step print a different random string. My current code is not working correctly, the only thing I'm getting a random item and the same index printed multiple times.

My output right now:

relax
2
2
2
2

How can I fix that and get a correct randomized output?

My code:

public static void main(String[] args) {
    int i;
    
    String Cofee[] = {"pick it","drink it","relax","put it in a cup",};
     
    java.util.Random randomGenerator = new java.util.Random();

    int x = Cofee.length;
    int y = randomGenerator.nextInt(x);
    
    String frase = Cofee[y] ;
    System.out.println(frase);
    
    for(i = 0; i < Cofee.length; i++)
        System.out.println(y);      
}    

2 Answers 2

1

You assign a value to y once, and you print y repeatedly. The value of y doesn't change. To do that, you would need to call randomGenerator.nextInt(x) for each iteration of the loop!

However, if you want to randomize and print the array, use:

public static void main(String[] args)  
{
    String[] coffee = {"pick it","drink it","relax","put it in a cup",};
    // this wraps the array, 
    // so modifications to the list are also applied to the array
    List<String> coffeeList = Arrays.asList(coffee);
    Collections.shuffle(coffeeList);
    
    for(String value : coffee)
        System.out.println(value);      
}

As an aside, don't use String coffee[], but use String[] coffee. Although Java allows putting the array type after the variable name, it is considered bad form.

Or use a list directly:

public static void main(String[] args)  
{
    List<String> coffeeList = Arrays.asList("pick it","drink it","relax","put it in a cup");
    Collections.shuffle(coffeeList);
    
    for(String value : coffeeList)
        System.out.println(value);      
}
Sign up to request clarification or add additional context in comments.

3 Comments

hi, thanks for the answer, as i said im new and i hope tis question isn't stupid, but this code: for(String value : coffeeList). I've never see this, is it a type of cycle FOR ? you gave all the words of the array list to the string value ?? can you explain ? please :D thanks agian for the help
@PasqualeMeo It is called an enhanced for loop (sometimes called for-each), see the second half of The Java™ Tutorials: The for Statement. It iterates over each element in the array (or list).
Love it ! Thnks !
1

For that, you can implement a shuffling algorithm.

It's not so scaring as it might sound at first. One of the famous classic shuffling algorithms, Fisher–Yates shuffle is relatively easy to grasp.

The core idea: iterate over the given array from 0 to the very last index, and for each index swap the element that corresponds to the randomly generated index between 0 and the current index (i) with the element under the current index.

Also, I would advise creating a separate array representing indices and shuffle it in order to preserve the array of string its initial state (you can omit this part and change the code accordingly if you don't need this).

That's how it might be implemented:

public static final Random RANDOM = new Random(); // we need an instance for random to generate indices

A Fisher–Yates shuffle implementation:

public static void shuffle(int[] arr) {
    for (int i = 1; i < arr.length; i++) {
        int j = RANDOM.nextInt(i + 1); // generating index in range [0, i]
        swap(arr, i, j);               // swapping elements `i` and `j`
    }
}

Helper-method for swapping elements:

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

Usage-example:

String[] coffee = {"pick it","drink it","relax","put it in a cup"};
        
int[] indices = new int[coffee.length];
for (int i = 0; i < indices.length; i++) indices[i] = i; // or Arrays.setAll(indices, i -> i); if you're compfortable with lambda expressions
        
shuffle(indices);
        
for (int i = 0; i < coffee.length; i++) {
    String next = coffee[indices[i]];
    System.out.println(next);
}

Output:

drink it
pick it
put it in a cup
relax

Comments

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.