I'm supposed to make the compare to method on my own. Here are the instructions
"Where s1 and s2 are Strings, and b is of type integer, b will store 0 if the two strings are equal, a negative value if s1 is less than s2 and a positive value if s1 is greater than s2. "
The program I've made doesn't work and I'm not sure why, one thing is that if the arrays equal each other, the program fails, and even if they don't, the program does not work. Thanks for your help...
public class CompareCharMethod {
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] s1 = new char[] {'a', 'p', 'p', 'l', 'e'};
char[] s2 = new char[] {'o', 'r', 'a', 'n', 'g', 'e'};
int answer = CompareTo(s1, s2);
System.out.println(answer);
}
public static int CompareTo(char[] s1, char[]s2)
{
if (s1==s2) {
return 0;
} else {
int i=0;
do {
if (s1[i] <s2[i]) {
return -1;
} else if (s1[i] >s2[i]) {
return 1;
} else {
i++;
}
} while (s1[i]==s2[i]);
}
return 999;
}
}