1

I want to make a program where you enter input in a for loop and after the iterations, a randomized string value from an array gets placed with it in the output message at the end. I do not want the string values to repeat, I want each value to only be produced once. How can I go about doing that? (the int values and the string values in the two arrays need to stay matched as well, so apples should always lead to liking he number 1, bananas the number 2 etc)

I want the output to be like:

Alex likes mangos and the number 3

John likes apples and the number 1

Jane likes bananas and the number 2

instead of:

Alex likes mangos and the number 3

John likes mangos and the number 3

Jane likes apples and the number 1

package example;
import javax.swing.JOptionPane;
import java.util.Random;
public class Example {

    public static void main(String[] args) {
        
        StringBuilder generator = new StringBuilder();
        
        for (int a=1; a<4; a++){
            
            String name = JOptionPane.showInputDialog(null, "Enter person " + a + "'s name");
            
            Random random = new Random();
            String [] fruit = {"apples", "bananas", "mangos"};
            int [] number = {1, 2, 3};
            int randomIndex = random.nextInt(fruit.length);
            
            generator.append(name).append(" likes ").append(fruit[randomIndex]).append(" and the number ").append(number[randomIndex]).append("\n");     
        }
        JOptionPane.showMessageDialog(null, generator);
    }
    
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kristie
  • 31
  • 6
  • 1
    Does this answer your question? [Generating Unique Random Numbers in Java](https://stackoverflow.com/questions/8115722/generating-unique-random-numbers-in-java) – M. Dudek Apr 30 '21 at 06:27

2 Answers2

1

You could create shuffled lists of fruits and numbers outside of your for loop and then get unique elements using their indices:

final StringBuilder generator = new StringBuilder();
final List<String> fruits = Arrays.asList("apples", "bananas", "mangos");
final List<Integer> numbers = Arrays.asList(1, 2, 3);

Collections.shuffle(fruits);
Collections.shuffle(numbers);

assert fruits.size() == numbers.size();

for (int i = 0; i < fruits.size(); i++) {
    final String name = JOptionPane.showInputDialog(null, "Enter person " + i + "'s name");

    generator.append(name)
        .append(" likes ")
        .append(fruits.get(i))
        .append(" and the number ")
        .append(numbers.get(i))
        .append("\n");
}

JOptionPane.showMessageDialog(null, generator);
stevecross
  • 5,588
  • 7
  • 47
  • 85
  • Although this does work with getting randomized values to not repeat, the arrays are no longer matched. (mangoes and the number 2 would match up instead of 3) I've used the `assert fruits.size() == numbers.size();` as you suggested but its still not working, is there a way I can fix that? – Kristie May 13 '21 at 08:17
0

You will need to maintain some sort of state to ensure you're not repeating numbers. The laziest way to do this would be to remove a value you're retreiving from the number array.

If you covert the number arrary into an Integer List and retreive the value using the pop method, it should work as expected.