// Names are stored here
final ArrayList<String> names = new ArrayList<String>();
// Get input from standard in
final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// Prompt for input
System.out.println("Enter names to sort: ");
// Get input
String currentLine;
while ((currentLine = in.readLine()) != null) {
// A blank line equals the end of input
if (currentLine.equals("")) {
break;
}
names.add(currentLine);
}
// Remember to close our input
in.close();
System.out.println("Sorting: " + names);
// Let Java do our sorting for us.
Collections.sort(names);
System.out.println("Sorted: " + names);