The assignment asks for three strings of alphabetical input (that is, letters and no numbers), then compare lexicographically and draw the middle one.
I found a similar concern here (Java: Three strings, lexicographic order), but can't comment to add my question. I sorted (for an instant) how to appropriately return the output, but now the code isn't giving any output and I don't know what I did wrong.
public static void main(String[] args)
{
printHeading();
String topString;
String middleString;
String bottomString;
Scanner in;
in = new Scanner(System.in);
System.out.println("Please enter a first word:");
while (!in.hasNext("[A-Za-z]+"))
{
System.out.println("Please use only alphabetic values.");
System.out.println("Please enter a first word:");
in.nextLine(); // Captures the first word
}
String firstWord = in.nextLine();
System.out.println("Please enter a second word:");
while (!in.hasNext("[A-Za-z]+"))
{
System.out.println("Please use only alphabetic values.");
System.out.println("Please enter a second word:");
in.nextLine(); // Captures the second word
}
String secondWord = in.nextLine();
System.out.println("Please enter a third word:");
while (!in.hasNext("[A-Za-z]+"))
{
System.out.println("Please use only alphabetic values.");
System.out.println("Please enter a third word:");
in.nextLine(); // Captures the third word
}
String thirdWord = in.nextLine();
if (firstWord.equalsIgnoreCase(secondWord) && secondWord.equalsIgnoreCase(thirdWord))
{
System.out.println();
System.out.println("The words are the same! Please try again.");
}
if (firstWord.compareTo(secondWord) > 0 && firstWord.compareTo(thirdWord) > 0)
{
topString = firstWord;
}
else if (firstWord.compareTo(secondWord) < 0 && firstWord.compareTo(thirdWord) > 0)
{
middleString = firstWord;
System.out.println();
System.out.println("The second word in lexicographic order is: " + middleString);
}
else
{
bottomString = firstWord;
}
if (secondWord.compareTo(firstWord) > 0 && secondWord.compareTo(thirdWord) > 0)
{
topString = secondWord;
}
else if (secondWord.compareTo(firstWord) < 0 && secondWord.compareTo(thirdWord) > 0)
{
middleString = secondWord;
System.out.println();
System.out.println("The second word in lexicographic order is: " + middleString);
}
else
{
bottomString = secondWord;
}
if (thirdWord.compareTo(secondWord) > 0 && thirdWord.compareTo(firstWord) > 0)
{
topString = thirdWord;
}
else if (thirdWord.compareTo(secondWord) < 0 && thirdWord.compareTo(firstWord) > 0)
{
middleString = thirdWord;
System.out.println();
System.out.println("The second word in lexicographic order is: " + middleString);
}
else
{
bottomString = thirdWord;
}