The problem
Write a program that prompts the user to enter three cities and displays them in ascending order. NOTE: Can't use sort() method or any type of arrays.
Breaking it down
The program states not to use Array.sort()
so instead we created a stream from strings and called the Stream.sorted()
method which will sort in ascending order. Finally to show the output we called Stream.forEach()
passing in System.out::println
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first city: ");
String s1 = input.nextLine();
System.out.print("Enter the second city: ");
String s2 = input.nextLine();
System.out.print("Enter the third city: ");
String s3 = input.nextLine();
input.close();
Stream.of(s1, s2, s3).sorted().forEach(System.out::println);
}
Output
Enter the first city: Birtch Creek
Enter the second city: Adrian
Enter the third city: York
Adrian
Birtch Creek
York