Answer the question
In order to leave comments, you need to log in
How can I make it so that the text is entered from the console, and was not set initially?
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
/**
Написать консольную программу, которая бы сортировала текст поданный ей на стандартный вход по алфавиту.
Варианты усложнения:
1.Программа должна игнорировать регистр при сортировке
2.Программа должна сортировать не по алфавиту, а по количеству символов в строке
*/
public class NewClass8 {
public static void main(String... args) {
String[] strings = {"We", "wish", "you", "a", "Merry", "Christmas"};
Collections.sort(Arrays.asList(strings), new MyComparator());
for (String string : strings) {
System.out.println(string);
}
}
}
class MyComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
Integer i1 = o1.length();
Integer i2 = o2.length();
return i1.compareTo(i2);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question