user12238056
user12238056

Reputation:

How can I sort strings into the correct alphabetical order using only if statements from my code?

I was wondering if anyone could assist me with trying to sort three string values into the correct alphabetical order using only if statements?

From my understanding the operator choice that I'm using is for integer values and not strings. Therefore, what operator can I use instead that applies for strings? Incase someone says skip if statements all together and use arrays I can't, if statements only.

MY CODE

    String p= "Matt";
    String m ="Jack";
    String o ="Sam";

   if (( p <= m ) && ( m <= o ) )
    {
        System.out.println("");
   } 
   else if (( p <= m) && ( m <= o ) )
   {
        System.out.println("");
   }
   else if (( o <= p ) && ( p <= m ) )
   {
        System.out.println("");
   }
   else if (( o <= m ) && ( m <= p ) )
   {
        System.out.println("");
   }
   else if (( m <= p ) && ( p <= o ) )
   {
        System.out.println("");
   }
   else
   {
        System.out.println("");
   }

Error I'm getting in BlueJ

Upvotes: 0

Views: 1891

Answers (2)

Maurice Perry
Maurice Perry

Reputation: 9650

How about:

    if (p.compareTo(m) <= 0) {
        if (m.compareTo(o) <= 0) {
            System.out.println(p);
            System.out.println(m);
            System.out.println(o);
        } else if (p.compareTo(o) <= 0) {
            System.out.println(p);
            System.out.println(o);
            System.out.println(m);
        } else {
            System.out.println(o);
            System.out.println(p);
            System.out.println(m);
        }
    } else {
        if (p.compareTo(o) <= 0) {
            System.out.println(m);
            System.out.println(p);
            System.out.println(o);
        } else if (m.compareTo(o) <= 0) {
            System.out.println(m);
            System.out.println(o);
            System.out.println(p);
        } else {
            System.out.println(o);
            System.out.println(m);
            System.out.println(p);
        }
    }

Upvotes: 0

Osama A.Rehman
Osama A.Rehman

Reputation: 932

You're trying to compare strings with <= which is not operable on strings, that's why you get the error stating bad operand type.

You should use string1.compareTo(string2) method to compare two strings. It returns 0 when two strings are equal, negative number if string1 < string2 and positive number if string1 > string2.

Given that, you should do something like this:

String p= "Matt";
String m ="Jack";
String o ="Sam";

if (( p.compareTo(m) <= 0 ) && ( m.compareTo(o) <= 0 ) )
{
    System.out.println("");
} 
else if (( p.compareTo(m) <= 0 ) && ( m.compareTo(o) <= 0 ) )
{
    System.out.println("");
}
else if (( o.compareTo(p) <= 0 ) && ( p.compareTo(m) <= 0 ) )
{
    System.out.println("");
}
else if (( o.compareTo(m) <= 0 ) && ( m.compareTo(p) <= 0 ) )
{
    System.out.println("");
}
else if (( m.compareTo(p) <= 0 ) && ( p.compareTo(o) <= 0 ) )
{
    System.out.println("");
}
else
{
    System.out.println("");
}

Upvotes: 2

Related Questions