Skip to content

Instantly share code, notes, and snippets.

@moderateepheezy
Created April 15, 2017 11:25
Show Gist options
  • Save moderateepheezy/c53158cb24ba53c3bce42725235e9e35 to your computer and use it in GitHub Desktop.
Save moderateepheezy/c53158cb24ba53c3bce42725235e9e35 to your computer and use it in GitHub Desktop.
Shuffle and pair in group of two.
public class MainClass {
public static void main(String[] args) {
Scanner lop = new Scanner(System.in);
List<Pair> pairList = new ArrayList<>();
System.out.println("Enter number total number of people to group");
int length = lop.nextInt();
lop.nextLine();
List<String> names = new ArrayList<>();
String s = "Please enter " + length + " names.";
System.out.println(s);
names = getNames(lop, names, 0, length);
Collections.shuffle(names);
int counter = 0;
for (int i =0; i< names.size(); i+=2){
Pair pair = new Pair(names.get(counter), names.get(counter+1));
pairList.add(pair);
counter = counter + 2;
}
names.clear();
for (Pair par : pairList) {
System.out.println(par.getKey() + " and " + par.getValue());
}
lop.close();
}
private static List<String> getNames(Scanner lop, List<String> names,
int index, int length) {
String s = lop.nextLine();
names.add(s);
if (index < (length - 1)) {
s = "Enter " + (length - index - 1) + " more names.";
System.out.println(s);
getNames(lop, names, ++index, length);
}
return names;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment