2

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
TGautier
  • 47
  • 1
  • 1
  • 8
  • It would make your code *much* easier to read if you'd format it more conventionally - and get rid of the excess brackets round the second condition in each of your tests. – Jon Skeet Jan 30 '14 at 19:02
  • The error message says it all: there are theoretical paths of execution where you're trying to use middleString without first initializing it. – Kevin Workman Jan 30 '14 at 19:05
  • If you just want to get the lexicographically "middle" string, and don't care how you do it, put the strings into an ArrayList, sort it and pull out the middle one. – Martin Dinov Jan 30 '14 at 19:12
  • Jon Skeet: Sorry, I will format better in the future. – TGautier Jan 30 '14 at 19:14
  • Martin Dinov: Wish I didn't have to care, but we have not gotten to ArrayLists in class yet and I want to show that I can program this with what we have been taught so far. Of all the options I believe the professor is looking for the string.compareTo method. – TGautier Jan 30 '14 at 19:16

5 Answers5

4

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).

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 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. – TGautier Jan 30 '14 at 19:13
  • @TGautier. How about now? – Mad Physicist Jan 30 '14 at 19:41
  • @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! – TGautier Jan 30 '14 at 19:50
  • Are you sure Arrays.sort(java.lang.String) exists as I am getting an exception??? – Neelam Jul 16 '16 at 18:23
  • @Neelam `Arrays.sort(java.lang.String)` certainly does not exist. `Arrays.sort(java.lang.String[])` does. – Mad Physicist Jul 17 '16 at 03:28
0

You have not initialised the variable middleString and using it in the end of the pgrm as

System.out.println("The second string in lexicographic order: " + middleString);

Simple initialise the variable as below and it will compile.

String  middleString ="";
Kick
  • 4,823
  • 3
  • 22
  • 29
0

Just initialize your 3 string topString, middleString, bottomString to empty like this:

String topString = "";
String middleString = "";
String bottomString = "";

Must compile.

Thrash Bean
  • 658
  • 4
  • 7
0

The logic here is wrong (I've reformatted a bit):

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; }

(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:

S1 > S2 and S1 > S3    S1 is the top string   
S1 > S2 and S1 < S3    S1 is the bottom string
S1 < S2 and S1 > S3    S1 is the middle string
S1 < S2 and S1 < S3    S1 is the bottom string

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.)

ajb
  • 31,309
  • 3
  • 58
  • 84
  • 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 Jan 30 '14 at 19:47
  • @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"? – Nirmal Oct 17 '15 at 22:31
  • @user3320018 I didn't say "you need four conditions". I said there are four cases that need to be considered. Either S1S2 (2 cases), and either S1S3 (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. – ajb Oct 18 '15 at 21:55
0

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:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class StringSorter {

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter the FIRST string:");
    String first = scanner.nextLine();
    System.out.println("Enter the SECOND string:");
    String second = scanner.nextLine();
    System.out.println("Enter the THIRD string:");
    String third = scanner.nextLine();

    List<String> strings = new ArrayList<String>();

    strings.add(first);
    strings.add(second);
    strings.add(third);

    System.out.println("Before sort:");
    for (String s : strings) {
      System.out.println(s);
    }

    Collections.sort(strings);

    System.out.println("After sort:");
    for (String s : strings) {
      System.out.println(s);
    }

    System.out.println("The Middle String is '" + strings.get(1) + "'");

    scanner.close();

  }

}

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:

Enter the FIRST string:
John
Enter the SECOND string:
JOHN
Enter the THIRD string:
Kevin
Before sort:
John
JOHN
Kevin
After sort:
JOHN
John
Kevin
The Middle String is 'John'
J Steven Perry
  • 1,711
  • 1
  • 17
  • 28