I've been having an issue with a lab that I'm doing for school and my professor specifically wants us to use the .compareTo() method for the following code:
The input that I'm doing is:import java.util.*; public class lab20 { static Scanner in = new Scanner(System.in); public static void main(String[] args) { String[] names = new String[20]; int size = 0; System.out.print("Enter a name, last name first(Enter 'END' to quit): "); String name = in.nextLine(); while (!name.equals("END") && names.length <= 20) { names[size] = name; System.out.print("Enter another name: "); name = in.nextLine(); size++; } for (int i = 0; i < size; i++) System.out.println(names[i]); sort(names, size); System.out.println(); for (int i = 0; i <size; i++) System.out.println(names[i]); } public static void sort (String[] names, int size) { int i = 0; int minPos; String temp; for (i = 0; i < size; i++) { minPos = minimunPosition(names, i, size); temp = names[i]; names[i] = names[minPos]; names[minPos] = temp; } } public static int minimunPosition(String[] names, int i, int size) { int minPos = i; int k; for (k = i+1; k < size; k++) { if (names[k].compareTo(names[minPos]) == -1) minPos = k; } return minPos; } }
Strickland, Bobby
Anderson, Brett
Keefer, Chris
Whymer, Josh
END
the output is the same as above without the END, obviously. I've tried using 1 instead of -1 as the return value of compareTo() but it's stays the same. I've changed the if statement to (name[k].charAt(0) < name[minPos].charAt(0)) but my professor said he'd take off points because he specifically want's me to use the compareTo(). Any help is greatly appreciated.