beginner Java programmer here. I am trying to compare three strings to each other, and have the system spit out the second/middle word in lexicographic order.
import java.util.*;
public class Ordered2
{
public static void main(String[] args)
{
String firstString, secondString, thirdString;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter three different strings.");
System.out.println("The string in the middle order lexicographically will be displayed.");
firstString = keyboard.nextLine();
secondString = keyboard.nextLine();
thirdString = keyboard.nextLine();
String topString, middleString, bottomString;
if (firstString.compareTo(secondString) > 0 && (firstString.compareTo(thirdString) > 0))
{ topString = firstString; }
else if (firstString.compareTo(secondString) < 0 && (firstString.compareTo(thirdString) > 0)) {
middleString = firstString; }
else { bottomString = firstString; }
if (secondString.compareTo(firstString) > 0 && (secondString.compareTo(thirdString) > 0)) {
topString = secondString; }
else if (secondString.compareTo(firstString) < 0 && (secondString.compareTo(thirdString) > 0)) {
middleString = secondString; }
else { bottomString = secondString; }
if (thirdString.compareTo(secondString) > 0 && (thirdString.compareTo(firstString) > 0)) {
topString = thirdString; }
else if (thirdString.compareTo(secondString) < 0 && (thirdString.compareTo(firstString) > 0)) {
middleString = thirdString; }
else { bottomString = thirdString; }
System.out.println("The second string in lexicographic order: " + middleString);
}
}
This does not compile, and tells me that middleString has not been initialized. Any help would be appreciated.