-3

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;
    }
}
SrThompson
  • 5,568
  • 2
  • 17
  • 25
  • 2
    You don't seem to take the length of the strings into account at all. You should also check that to make sure you don't index past the end of the strings. Additionally your method never returns 0 (i.e. the strings equal) unless the arrays are the exactly same object. Note that `s1 == s2` compares the array references, not the actual contents. – Matti Virkkunen Apr 26 '18 at 22:37
  • 1
    Aside from the above mentioned comment, I would firstly suggest to fix your code snippet. Secondly, you mention that you need to have the Strings checked for equality. Equality meaning what exactly? The actual content or the length of each? Also when you say it does not work, what do you mean by that? Please clarify a bit more in order for someone to have enough info to help you out. – akortex Apr 26 '18 at 22:38
  • Please read [How do I ask a question that is answerable?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. **Doesn't work** means absolutely nothing useful to anyone reading your question. It is implicit that something does not work or you would not be asking a question here. –  Apr 27 '18 at 00:19
  • [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). Your question can be answered very quickly and easily with your step-debugger. You should always try and solve your problems with a step debugger before coming to StackOverflow. –  Apr 27 '18 at 00:19
  • [Questions asking for *homework help* must include a summary of the work you've done so far to solve the problem, and **a detailed description of the difficulty you are having** solving it.](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) *does not work*, *please help me* are not acceptable. –  Apr 27 '18 at 00:24

2 Answers2

-1

This should be what youre looking for I interpreted it as lexicographical order.

public static int CompareTo(char[] s1, char[]s2)
  {
      if (s1==s2)
        return 0;
      int n = s1.length>=s2.length ? s2.length : s1.length;
      for(int i=0;i<n;i++){
        if(s1[i]<s2[i])
          return -1;
        else if(s1[i]>s2[i])
          return 1;
      }
      if(s1.length==s2.length)
          return 0;
      return s1.length>s2.length ? 1 : -1;
  }
Turan
  • 302
  • 2
  • 9
  • Please read [What types of questions should I avoid answering?](https://meta.stackoverflow.com/questions/260087/add-what-types-of-questions-should-i-avoid-answering-to-the-help-center) before attempting to answer more questions. –  Apr 27 '18 at 00:18
-3

You can Try this:

public static void main(String[] args) {
    String s1 = "apple";
    String s2 = "orange";
    int answer = myCompareTo(s1.toCharArray(), s2.toCharArray());
    System.out.println(answer);
}

public static int myCompareTo(char[] s1, char[]s2) {
    int len1 = s1.length;
    int len2 = s2.length;
    int lim = Math.min(len1, len2);
    char v1[] = s1;
    char v2[] = s2;

    int k = 0;
    while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}
  • Please read [What types of questions should I avoid answering?](https://meta.stackoverflow.com/questions/260087/add-what-types-of-questions-should-i-avoid-answering-to-the-help-center) before attempting to answer more questions. –  Apr 27 '18 at 00:18