I'm trying to create my own "int compareTo(String,String)
" method. I was wondering what should I return if I find any of the strings are null? throw an exception? return -1 is a possible result for compareTo...
public int compare(String st1, String st2){
if (st1 == null) || st2 == null ) return ..
...
//logic
...
}
compareTo definition:
public int compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)