I have the following code, and I have sort the array list alphabetically in the main method, as the user inputs his strings. Here is my code:
import java.util.Scanner;
import java.util.ArrayList;
class Main{
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
Scanner scan = new Scanner(System.in);
String name;
do{
System.out.println("Enter the next name: ");
name = scan.nextLine();
String toUpperCase = titleCase(name);
if(!toUpperCase.equals("Stop")){
names.add(toUpperCase);
}
} while(!name.equalsIgnoreCase("STOP"));
System.out.println(names.toString());
}
public static String titleCase(String s){
String output = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
return output;
}
}
Please don't give any generic answers, I've been struggling with this for a while now. If the answer seems simple to you, it probably isn't for me.
TreeSet? Otherwise, write a sort method.