user3401578 user3401578 - 1 month ago 46
Java Question

how do i write this method that returns the larger string?

I am completely new to programming (java) and our teacher asks us to make some excercises in my programming lab.

Now I can't seem to find out how to write the correct method.
This is the question:
Write a method max that has two string parameters and returns the larger of the two.

I have

String string1, string2;
public max Strings(string1.length(), string2.length())
{
if (string1.length()>string2.length)
{
return string1;
else if (string1.length()<string2.length)
return string2;
}
}

Answer
public String max(String a, String b) {
    return (a.length() > b.length()) ? a : b;
}

There is the condition that both strings are of equal length which is not tested for, also null and empty strings,