Earlier, I posted about a program that wasn't working properly. When I enter names, I need it to randomize and pair two names together. My code follows
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How many names would you like to enter?");
int n = scan.nextInt();
System.out.println("Enter the " + n + " names: ");
String[] names = new String[n];
for (int i = 0; i < names.length; i = i + 1) {
names[i] = scan.nextLine();
}
List<String> AllNames;
// Prints the names in a list
AllNames = new ArrayList<>(Arrays.asList(names));
for (int i = 0; i < names.length; i = i + 2) {
System.out.print(names[i]);
if (i < names.length) {
System.out.print(" " + "&" + " " + names[i + 1]);
}
System.out.println();
}
Random rand = new Random();
Collections.shuffle(AllNames, rand);
scan.close();
}
The output received is:
' & a'
'b & c'