0

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;
    }
Wraith11B
  • 1
  • 1
  • Please give example inputs and the expected and actual outputs. – Andy Turner Sep 23 '15 at 22:12
  • Is there a good reason (e.g. restrictions in the assignment) not simply to write the elements into an array, sort the array, then pick the middle element? – Andy Turner Sep 23 '15 at 22:13
  • Basically, the assignment is to use if/else/elseif and logic to use the code we already know. Going too far "beyond" what is strictly in the assignment is discouraged. Example inputs would be something like "abcd" "Glad" "zulu" (in no particular order); by normal ordinal systems, the order would be Glad, abcd, zulu. I (am assuming) the actual expected order should be "abcd, Glad, zulu" and have the program return "Glad." I had it (just like this, I was fairly certain) and then tried adding the "IgnoreCase" bit to all of the '.compareTo' , but it decided to suddenly not return anything. – Wraith11B Sep 24 '15 at 03:56

1 Answers1

0

Your string comparison statements are incorrect - you need to check them and rewrite it. Here is another way to do it:

import java.util.Scanner;

public class FindMiddleWord 
{
public static void main(String[] args)
{

    String[] wordArray = new String[3];

    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();
    wordArray[0] = firstWord;

    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();
    wordArray[1] = secondWord;

    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();
    wordArray[2] = thirdWord;

    String temp;
    int  i,j = 0;
    for (i = 0; i < wordArray.length; i++) {
        for (j = 0; j < wordArray.length; j++) {
            if (wordArray[i].compareToIgnoreCase(wordArray[j]) < 0) {
                temp = wordArray[i];
                wordArray[i] = wordArray[j];
                wordArray[j] = temp;
            }
        }
    }       
    System.out.println("The Middle Word is : "+wordArray[1]);       
  }
}
PNB
  • 41
  • 4
  • I would love to use the array function, but it's outside of the scope of the assignment, unfortunately. How are the comparison statements wrong? Could you point out where I made the mistake? – Wraith11B Sep 24 '15 at 15:59