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>();