0

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'
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226

2 Answers2

0

You need to first shuffle the list and then printout them.

Edit: Demonstrated duplicate name detection, error for odd n.

import java.util.*;

public class TestX {
    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();
        scan.nextLine(); // to capture new line after the n

        if (n % 2 != 0) {
            System.out.println("ERROR!!!");
            System.exit(0);
        }

        System.out.println("Enter the " + n + " names: ");
        List<String> nameList = new ArrayList<>();

        for (int i = 0; i < n; i++) {
            String name = scan.nextLine();

            if (nameList.contains(name)) {
                System.out.println("Already exists " + name);
            } else {
                nameList.add(name);
            }
        }

        scan.close();

        Collections.shuffle(nameList);

        for (int i = 0; i < nameList.size(); ) {
            if ((i + 1) >= nameList.size()) {
                System.out.println(nameList.get(i));
                i++;
            } else {
                System.out.println(nameList.get(i) + " & " + nameList.get(i + 1));
                i += 2;
            }
        }

    }
}
shakhawat
  • 2,393
  • 17
  • 32
0

You need to call nextLine() after nextInt() (to consume one trailing new line character). You don't seem to need an array (since you're using a List anyway). You should shuffle before you try to print your randomized pairs. And you need to check i + 1 against the length (or size, or n) before accessing the element at i + 1. Something like,

Scanner scan = new Scanner(System.in);
System.out.println("How many names would you like to enter?");
int n = scan.nextInt();
scan.nextLine();
System.out.println("Enter the " + n + " names: ");
List<String> names = new ArrayList<>();
for (int i = 0; i < n; i++) {
    names.add(scan.nextLine());
}
Collections.shuffle(names);
for (int i = 0; i < n; i += 2) {
    System.out.print(names.get(i));
    if (i + 1 < n) {
        System.out.print(" & " + names.get(i + 1));
    }
    System.out.println();
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226