Q:

Splitting and Comparing one String (Java)

I have been testing different method for a while and this is what I got. I just can't figure out how to solve this.
/** Return true iff s has an odd number of characters and
 *  the substring before the middle character equals the substring
 *  after it.
 * Examples: For s = "" return false
 * For s = "b" return true
 * For s = "xbx" return true
 * For s = "xxxx" return false
 * For s = "hellohello" return false
 * For s = "hello!hello" return true */
public static boolean isDoubled(String s) {
    // TODO 1. There is no need for a loop. Do not use a loop.
    // In all methods, use s1.equals(s2) and NOT s1 == s2 to test
    // equality of s1 and s2.
    int midLen = s.length() / 2;
    if (midLen == 0) return false;
    String[] parts = {s.substring(0, midLen - 1), s.substring(midLen + 1)};
    String part1 = parts[0];
    String part2 = parts[1];
    if ((s.length() % 2 == 0) && (part1.equals(part2))) {
        return true;
    }
    return false;
}

A:

You have mistake with using substring function:
public static boolean isDoubled(String s) {
    if (s.length() % 2 == 0)
        return false;
    if (s.length() == 1)
        return true;

    int mid = s.length() / 2;
    return s.substring(0, mid).equals(s.substring(mid + 1));
}

