Java: Three strings, lexicographic order

22,900

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 to null or an empty string. Replace String topString, middleString, bottomString; with

String topString = null;
String middleString = null;
String bottomString = null;

Additionally, you may want to use some of Java's built-in sorting functionality to do the sorting for you:

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[] array = new String[] {firstString, secondString, thirdString};
    Arrays.sort(array);
    System.out.println("The second string in lexicographic order: " + array[1]);
}
}

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 use Arrays.sort(array, String.CASE_INSENSITIVE_ORDER).

Share:
22,900
TGautier
Author by

TGautier

Computer Science undergraduate currently studying at Hutchinson Community College. Transferring to Wichita State University in 2015. Tech Support Assistant for USD 405 school district.

Updated on January 31, 2020

Comments

  • TGautier
    TGautier over 3 years

    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.

  • TGautier
    TGautier over 9 years
    This helps, but middleString remains 'null' at the end of the program. Example, entering 'zebra' 'apple' 'honey' as my respective strings produces this result: The second string in lexicographic order: null.
  • Mad Physicist
    Mad Physicist over 9 years
    @TGautier. How about now?
  • TGautier
    TGautier over 9 years
    Thank you, this helped. My logic was backwards. I was checking to see if secondString is greater than firstString, when logically I was trying to check originally if firstString is greater than secondString. Flipping the signs all through the code fixed my issue. Thanks everyone for your help!
  • TGautier
    TGautier over 9 years
    @MadPhysicist Thank you, I'm sure it works but I am trying not to use Arrays or TreeSet or anything to solve this issue. I am only trying to accomplish this with what we have been taught in class so far. I appreciate the help!
  • Nirmal
    Nirmal over 7 years
    @ajb I found the wrong condition in your list of four conditions...so we don't need 4 condition for each string do we? what do you mean by "you need four condition for each"?
  • ajb
    ajb over 7 years
    @user3320018 I didn't say "you need four conditions". I said there are four cases that need to be considered. Either S1<S2 or S1>S2 (2 cases), and either S1<S3 or S1>S3 (2 cases), and 2*2=4 cases. That doesn't mean you have to have four conditions in your code. But however you write the code, it has to work for all 4 cases. The OP's attempted code only made it work for 3 of the 4.
  • Neelam
    Neelam almost 7 years
    Are you sure Arrays.sort(java.lang.String) exists as I am getting an exception???
  • Mad Physicist
    Mad Physicist almost 7 years
    @Neelam Arrays.sort(java.lang.String) certainly does not exist. Arrays.sort(java.lang.String[]) does.