0

So I am trying to write a code to find the middle string of a set of given strings, in this case 3. By middle I mean whichever is the middle in lexicographic order. The code I have written compiles and works no problem but with certain combinations of strings it doesn't want to work properly. I ran some tests and I posted in comments below which combinations of strings were given me an error. The numbers by the tests are the actual order of the strings. I was told that some of the combinations aren't satisfying all of the boolean statements but I don't really see how. I know it's a simple solution but any help would be appreciated.

import java.util.Scanner;
public class MiddleString   {

    public static void main(String[] args)  {

        Scanner in = new Scanner (System.in); 

        System.out.println("Please enter the first string to compare");
            String str1 = in.nextLine();

        System.out.println ("Now enter the second string");
            String str2 = in.nextLine();

        System.out.println ("Good! And finally, the third");
            String str3 = in.nextLine();

        if ((str1.compareTo(str3) < 0) && (str1.compareTo(str2) < 0) && (str2.compareTo(str3) < 0))
            System.out.println("In lexicographic order, the string in the middle will be " + str2);
        else if ((str3.compareTo(str1) < 0) && (str1.compareTo(str2) < 0) && (str3.compareTo(str2) < 0))
            System.out.println("In lexicographic order, the string in the middle will be " + str1);
        else if ((str1.compareTo(str2) < 0) && (str3.compareTo(str2) < 0) && (str1.compareTo(str3) < 0))
            System.out.println("In lexicographic order, the string in the middle will be " + str3);
    }
}

// TESTS
// str1|str2|str3|result
//  A  |  B |  C |pass  123
//  B  |  C |  A |pass  231
//  M  |  A |  Z |FAIL  213
//  A  |  Z |  M |pass  132
//  Z  |  M |  A |FAIL  321
//  Z  |  A |  M |FAIL  312
//
M. G
  • 5
  • 1
  • 4
  • 1
    If `(str1.compareTo(str2) < 0) && (str2.compareTo(str3) < 0)`, it's redundant to check that `str1.compareTo(str3) < 0`. But you do need to check if `(str1.compareTo(str2) > 0) && (str2.compareTo(str3) > 0)`. Or just check if `str1.compareTo(str2) == str2.compareTo(str3)`. – shmosel Jun 04 '18 at 23:17

1 Answers1

0
import java.util.Scanner;

public class MiddleString {

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    String result = "";

    System.out.println("Please enter the first string to compare");
    String str1 = in.nextLine();

    System.out.println("Now enter the second string");
    String str2 = in.nextLine();

    System.out.println("Good! And finally, the third");
    String str3 = in.nextLine();

    if ((str1.compareTo(str2) < 0)) 
    {
        if ((str2.compareTo(str3) < 0)) 
        {
            result = str2;
        } else 
        {
            result = str3;
        }
    } else 
    {
        result = str1;
    }
    System.out.println("In lexicographic order, the string in the middle will be " + result);
}
}
Christophe Chenel
  • 1,802
  • 2
  • 15
  • 46