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.
The logic here is wrong (I've reformatted a bit):
(I'm going along with your approach, which I think can be made to work with some tweaking.) I'm going to call the strings S1, S2, S3. Assuming none of the strings are equal, there are four cases you need to consider. I've listed those, along with what the above code is doing:
One of those is wrong. See it?
(I haven't checked the other two
if
's. You should do the same thing, looking at four cases for each one.)Just initialize your 3 string topString, middleString, bottomString to empty like this:
Must compile.
The Java compiler does not know which branch of an
if
statement will be executed. That means that if you initialize a variable in one branch but not the other, the variable is not guaranteed to have a value assigned to it. In your code, all of the variables will of course be initialized, but the compiler has no way of knowing this, hence your error. You can just initialize the three tonull
or an empty string. ReplaceString topString, middleString, bottomString;
withAdditionally, you may want to use some of Java's built-in sorting functionality to do the sorting for you:
Arrays.sort()
sorts the strings for you. Taking the second (index 1) string out of the sorted array gives you the middle string. If you want to sort using case-insensitive ordering, you can useArrays.sort(array, String.CASE_INSENSITIVE_ORDER)
.If I understand your scenario, you are only interested in Strings, you can take advantage of the Natural Order of Strings and use JDK classes to help you out:
When I ran this in Eclipse (go ahead, paste it in and try it!) using John, JOHN and Kevin as the names, I got this result:
You have not initialised the variable
middleString
and using it in the end of the pgrm asSimple initialise the variable as below and it will compile.