In: Computer Science
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE!
Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
import java.util.Scanner;
public class MiddleString {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first string: ");
String s1 = in.nextLine();
System.out.print("Enter second string: ");
String s2 = in.nextLine();
System.out.print("Enter third string: ");
String s3 = in.nextLine();
if ((s1.compareTo(s2) >= 0 && s1.compareTo(s3) <= 0) || (s1.compareTo(s3) >= 0 && s1.compareTo(s2) <= 0)) {
System.out.println(s1 + " is the middle string");
} else if ((s2.compareTo(s1) >= 0 && s2.compareTo(s3) <= 0) || (s2.compareTo(s3) >= 0 && s2.compareTo(s1) <= 0)) {
System.out.println(s2 + " is the middle string");
} else {
System.out.println(s3 + " is the middle string");
}
}
}