0

This is my code at the the moment, basically I have an array of string, then I ask the user how big the list of strings he wants, the only problem i have is it generates duplicates, how do I fix that?

public static void main(String arg[]){
    //Object to get user input
    Scanner in = new Scanner(System.in);

    //String array
    String[] strings = {"Kelder", "Slotgracht", "Raadsheer", "Dorp", "Militie", "Werkplaats", "Bureaucraat", "Feest", "Tuin", "Houthakker", "Geldschieter", "Verbouwing", "Smidse", "Spion", "Dief", "Troonzaal", "Raadszaal", "Festival", "Laboratorium", "Bibliotheek", "Markt", "Mijn", "Heks", "Avonturier"};

    System.out.print("Hoeveel kaarten wil je?: ");
    //Get user input
    int numberOfTest = in.nextInt();

    for(int i = 0; i < numberOfTest; i++){
        int index = (int)(Math.random() * 10);
        System.out.println(strings[index]);
    }
}
  • 2
    Well, in order to avoid duplicates, you must know which strings were already picked. You could construct a `Set` with all the values, pick a random element and then **remove it from the set**. Or just shuffle the whole set using `Collections.shuffle` and then pick subsequent elements. – MC Emperor May 25 '19 at 22:10

2 Answers2

1

You can do this:

import java.util.Scanner;
import java.util.ArrayList;

class Main {

    public static void main(String arg[]){
    //Object to get user input
    Scanner in = new Scanner(System.in);

    //String array
    String[] strings = {"Kelder", "Slotgracht", "Raadsheer", "Dorp", "Militie", "Werkplaats", "Bureaucraat", "Feest", "Tuin", "Houthakker", "Geldschieter", "Verbouwing", "Smidse", "Spion", "Dief", "Troonzaal", "Raadszaal", "Festival", "Laboratorium", "Bibliotheek", "Markt", "Mijn", "Heks", "Avonturier"};

    // ArrayList to store string selected already
    ArrayList<String> selectedBefore = new ArrayList<String>();

    System.out.print("Hoeveel kaarten wil je?: ");
    //Get user input
    int numberOfTest = in.nextInt();

    for(int i = 0; i < numberOfTest; i++){
        int index = (int)(Math.random() * strings.length);

        // Keep checking if the string has been selected before
        while(selectedBefore.contains(strings[index]))
        {
          index = (int)(Math.random() * strings.length);
        }

        //store the string that was selected by Math.random() before:  
        selectedBefore.add(strings[index]);

        System.out.println(strings[index]);
    }
}

}

This solution doesn't change much of your original code structure, but added:

A selectedBefore ArrayList to help store the string that has been selected already.

A while loop to keep checking whether the string has been selected before, if it has, pick a new index with Math random until it picks the one that hasn't been selected before. Since this is a small list, the algorithm works well, if it's a big list it will hit the performance of the program and you would need to change the algorithm of the program.

You probably also need to limit the user input to the maximum number available in the string array so that it doesn't get an index out of bound exception.

In addition, you can also initialize the ArrayList in this manner to save memory since you know the size of it is going to be equal to length of your string array:

ArrayList<String> selectedBefore = new ArrayList<String>(strings.length);

instead of this in the previous unedited answer:

ArrayList<String> selectedBefore = new ArrayList<String>();
hiew1
  • 1,394
  • 2
  • 15
  • 23
  • the problem with this is that it generates a list of strings based on only the first 10 items in the arraylist, and it also won't generate more than 10 – Dave Brouwer May 25 '19 at 23:20
  • I edited the answer. It's the Math.random() method. Let's say if we write the Math.random() * 10, it will pick a random number between 0 to 9. Now it's Math.random() * strings.length, it will pick a random number between 0 and the length of your strings array, which is 23. You have 24 strings numbered from 0 to 23. – hiew1 May 25 '19 at 23:35
0

Here's an example of what MC Emperor suggested:

public static void main(String [] args) {  
    List<String> strings = Arrays.asList("Kelder", "Slotgracht", "Raadsheer", "Dorp", "Militie", "Werkplaats", "Bureaucraat", "Feest", "Tuin", "Houthakker", "Geldschieter", "Verbouwing", "Smidse", "Spion", "Dief", "Troonzaal", "Raadszaal", "Festival", "Laboratorium", "Bibliotheek", "Markt", "Mijn", "Heks", "Avonturier");
    Collections.shuffle(strings);

    Scanner in = new Scanner(System.in);
    System.out.print("Hoeveel kaarten wil je?: ");
    int numberOfTest = in.nextInt();

    if (numberOfTest >= 0 && numberOfTest < strings.size()) {
        for(int i = 0; i < numberOfTest; i++){
            System.out.println(strings.get(i));
        }
    }    
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40