简体   繁体   中英

Comparing character strings

So I have a method which compares two character arrays that are inserted (s and t where s is the character array which should return true if is smaller ) and the aim is to find out whether one is less than the other (in dictionary order) to the first n characters.

public boolean lessCheck(char[] s, char[] t, int n) {
            int q = 0;
            if (t.length < n || s.length < n) {
                if (s.length != t.length) {
                    if (t.length < s.length) {
                        q = t.length;
                    } else {
                        q = s.length;
                    }
                } else {
                    q = t.length;
                }
            } else {
                q = n;
            }

            for (int i = 0; i < q; i++) {
                if (t[i] != s[i]) {
                    if (t[i] > s[i]) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }

            if (equal(s, t, n)) {
                return false;
            } else {
                return true;
            }
        }

The issue that I'm having is that whenever I sub

s = bind, t = bin and n = 7 I get true.

This should return false as bind is less than bin in dictionary order...

Any help?

s = bind, t = bin and n = 7 I get true.

when called q=3

right?

the for loop compares the 3 chars b,i,n .

right?

The final if statement uses the equal method. Lenghts of bind and bin are different and lower than n.

equal return false, right?

The final result you got is true, from the else block of the last if .

right?

The String class defines a compareTo method which should met your requirements:

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)

new String("bind").compareTo("bin")>0
new String("bin").compareTo("zoo")<0
new String("bin").compareTo("bin")==0

The problem seems to be related to this piece of code:

        for (int i = 0; i < q; i++) {
            if (t[i] != s[i]) {
                if (t[i] > s[i]) {
                    return true;
                } else {
                    return false;
                }
            }
        }

that should be

        for (int i = 0; i < q; i++) {
            if (t[i] != s[i]) {
                if (t[i] > s[i]) {
                    return true;
                } else if (t[i] < s[i]) {
                    return false;
                }
            }
        }

In fact, if the condition t[i] > s[i] is NOT true, you still need to check whether t[i] < s[i] in order to return false.

I think a more compact version of your algorithm would be:

public static boolean lessThan(char[] s, char[] t, int n) {

    int minLength = Math.min(Math.min(s.length, t.length), n);

    for (int i = 0; i < minLength; i++) {
        if (s[i] < t[i]) {
            return true;
        } else if (s[i] > t[i]) {
            return false;
        }
    }

    return (minLength < n && s.length < t.length) ? true : false;
}

I hope that helps!

Your equals method returns false because the arrays are not the same length, then, because equals returned false , your lessThan method returns true .

There is no point in calling equals method after you compared almost all the characters. Instead of calling equals method, just check if s is shorter than t:

return s.length() < t.length();

If the subarrays are equal and one just shorter than the other, it means it will be placed before the longer array in the dictionary.

Sorry couldn't resist the logic to find q. There is no need to find this q.

try this:

public boolean lessThan(char[] s, char[] t, int n) {
    int i = 0, j = 0, q = 0;
    while (i < s.length && i < t.length && (i < n)) {
        if (s[i] > t[i])
            return false;
        i++;
    }
    if (s.length > t.length)
        return false;
    else if (n < i)
        return true;
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM