I have the following code which takes names and stores them in an 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);
}
public static String titleCase(String s){
String output = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
return output;
}
}
I need to sort the ArrayList alphabetically by name without using Collections.
sortList(List l)and implement a sorting algorithm. If this is not the expected answer: Let us know where your problem is.