I have a list of 4 names that I want to divide into 2 groups of 2 each.
//the variables are defined by user input
String name1=null, name2=null, name3=null, name4=null;
//Now i need something to choose 2 random names and print them out
System.out.println("Team A = name 1, name 2");
System.out.println("Team B = name 3, name 4");
etc.
It needs to be completely random. For when I add more names. It should take different group each time, and a name can only be used once.
I have tried with this:
List<String> list = new ArrayList<>();
list.add(name1);
list.add(name2);
list.add(name3);
list.add(name4);
selecto obj = new selecto();
System.out.println(obj.getRandomElement(list));
}
public int getRandomElement(List<Integer> list)
{
Random rand = new Random();
return list.get(rand.nextInt(list.size()));
This works perfectly with the integer but not strings.