Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

QUESTION: in Java, write a program that reads in three strings and prints them i

ID: 3648153 • Letter: Q

Question

QUESTION:
in Java, write a program that reads in three strings and prints them in lexicographically sorted order:
Please enter three strings:
Tom
Dick
Harry
The inputs in sorted order are:
Dick
Harry
Tom


(my code works as expected for these three strings but doesn't work for others, I keep getting a null value for the middle string when sorted.) Why?


I have this so far..
CLASS:

package practicesummer1;

public class PracticeSummer1 {

/**
* @param args the command line arguments
*/
String string1, string2, string3, first, second, third;

public PracticeSummer1(String string1a, String string2a, String string3a)
{
string1 = string1a;
string2 = string2a;
string3 = string3a;
}

public String getString1()
{

if (string1.compareTo(string2) < 0 && string1.compareTo(string3) < 0)
first = string1;
else if (string2.compareTo(string1) < 0 && string2.compareTo(string3) < 0)
first = string2;
else if (string3.compareTo(string1) < 0 && string3.compareTo(string2) < 0)
first = string3;
return first;

}

public String getString2()
{
if (string1.compareTo(string2) < 0 && string1.compareTo(string3) > 0)
second = string1;
if (string2.compareTo(string1) < 0 && string2.compareTo(string3) > 0)
second = string2;
if (string3.compareTo(string1) < 0 && string3.compareTo(string2) > 0)
second = string3;
return second;
}

public String getString3()
{
if (string1.compareTo(string2) > 0 && string1.compareTo(string3) > 0)
third = string1;
if (string2.compareTo(string1) > 0 && string2.compareTo(string3) > 0)
third = string2;
if (string3.compareTo(string1) > 0 && string3.compareTo(string2) > 0)
third = string3;
return third;
}

}


TESTER:

package practicesummer1;
//import java.util.Scanner;

/**
*
*
*/
public class tester {

public static void main(String[] args)
{
//// Scanner sc = new Scanner(System.in);
////
//// System.out.println("Please enter three strings");
//// String string1 = sc.next();
//// String string2 = sc.next();
//// String string3 = sc.next();

PracticeSummer1 test = new PracticeSummer1("Tom", "Dick", "Harry");
System.out.println(test.getString1());
System.out.println(test.getString2());
System.out.println(test.getString3());

}
}

Explanation / Answer

Hi, Please try the below program, which add the input string in a List and uses the Collections Sort method to sort the list , the program avoids complexity and uses Java 5 features. import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class SortString { List stringList = new ArrayList(); void addString(String s) { this.stringList.add(s); } void sortString() { Collections.sort(this.stringList); System.out.println("Sorted String "); for (String s : this.stringList) System.out.println(s); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); SortString sortstring = new SortString(); System.out.println("Please enter String 1"); sortstring.addString(sc.next()); System.out.println("Please enter String 2"); sortstring.addString(sc.next()); System.out.println("Please enter String 3"); sortstring.addString(sc.next()); sortstring.sortString(); } } Output: Please enter String 1 Tom Please enter String 2 Dick Please enter String 3 Harry Sorted String Dick Harry Tom Hope it helps.