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

main class = http://codepad.org/LnY51Zzj Link List class = http://codepad.org/o9

ID: 3653204 • Letter: M

Question

main class = http://codepad.org/LnY51Zzj

Link List class = http://codepad.org/o92XUhN1

Link class = http://codepad.org/I3tpIzOn

input file = http://codepad.org/a7nywNnP


i get an error at if (mystates[i] > mystates[i + 1])

(The operator > is undefined for the argument type(s) java.lang.String,)





public void sort(String [] mystates)

{

for (int j = mystates.length -1 ; j>0 ; j--)

{

boolean swapped = false;

for (int i = 0; i < j; i++)

{

if (mystates[i] > mystates[i + 1])

{

String t = mystates[i];

mystates[i] = mystates[i];

mystates[i] = t;

swapped = true;

}

}

if (swapped == false)

{

break;

}

}

}

Explanation / Answer

If you try to use the numerical less than (<) to compare strings.
The absolute best way to compare strings is to use a Comparator class.
There is a routine compareTo built into the String class that will let you do comparisons.

Wrong:

if (mystates[i] > mystates[i + 1])

Right:

if( mystates[i].compareTo(mystates[i + 1]) > 0)