I am developing a program to perform selection sort on a list of strings. The input for the program is 6 strings. The output for the program should be the list of strings sorted by their last character.
Here is what I have tried
import java.util.Arrays;
import java.util.Scanner;
import java.util.List;
import java.util.Comparator;
public class Exercise_20_21 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 6 strings: ");
// Step 1: proccess strings from input
String data = input.next();
String[] list = data.split(" ");
for(int i=0; i < list.length; i ){
System.out.print(list[i] " ");
}
selectionSort(list, new Comparator<String>(){
@Override
public int compare(String w1, String w2){
if(w1.charAt(0) > w2.charAt(0)){
return -1;
}
else if( w1.charAt(0) < w2.charAt(0)){
return 1;
}
else {
return 0;
}
}
});
for(int i=0; i < list.length; i ){
System.out.print(list[i] " ");
}
}
public static <E> void selectionSort(E[] list, Comparator<? super E> comparator) {
Arrays.sort(list,comparator);
}
}
Expected:
Enter 6 strings: red blue green yellow orange pink
red blue orange pink green yellow
Actual:
Enter 6 strings: red blue green yellow orange pink
red red
Any help with this problem would be greatly appreciated. Thank you.
CodePudding user response:
The reason why your output shows you only red red
is because you're reading a line with next()
instead of nextLine()
.
Your input string has its elements separated with a space, so your Scanner
instance will stop once it reads the first word red. You then print your array with only one element (red) and then you print the sorted array (still with the only element red).
You could fix your code by replacing that next()
call with a nextLine()
and shorten your Comparator
like so:
public class Main {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 6 strings: ");
// Step 1: process strings from input
String data = input.nextLine();
System.out.println(data);
String[] list = data.split(" ");
System.out.println("Printing the strings");
for (int i = 0; i < list.length; i ) {
System.out.println(list[i] " ");
}
System.out.println("\nPrinting the sorted strings");
selectionSort(list, Comparator.comparing(s -> s.charAt(s.length() - 1)));
for (int i = 0; i < list.length; i ) {
System.out.println(list[i] " ");
}
}
public static <E> void selectionSort(E[] list, Comparator<? super E> comparator) {
Arrays.sort(list, comparator);
}
}
Output
CodePudding user response:
The problem with your code is not with the comparator itself. You would want to use input.nextLine()
for being able to read an entire line. Using next
will read only until the next delimiter (which is space in your case).
For the comparator part, Java already provides a comparator for comparing strings and other objects. This is Comparator.naturalOrder()
and can be used as such:
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 6 strings: ");
// Step 1: proccess strings from input
String data = input.nextLine();
String[] list = data.split(" ");
System.out.println(Arrays.toString(list));
selectionSort(list, Comparator.naturalOrder());
for (String s : list) {
System.out.print(s " ");
}
}
public static <E> void selectionSort(E[] list, Comparator<? super E> comparator) {
Arrays.sort(list, comparator);
}