-1

I want to check if every character in a string is the inverse of the other character in the second. By inverse I mean uppercase and lowercase.

For example these strings:

  1. Ahh7h
  2. aHH7H

The result will be true

I wrote this code but the result is always false. Why?

public boolean checkString(String serverString, String clientString) {
    if (serverString.length() != clientString.length())
        return false;
    else
        for (int i = 0; i < clientString.length(); i++) {
            if ((clientString.charAt(i) >= '0' && clientString.charAt(i) <= '9')
                    && (clientString.charAt(i) != serverString.charAt(i)))
                return false;
            else if (clientString.charAt(i) >= 'A'
                    && clientString.charAt(i) <= 'Z') {
                if ((int) clientString.charAt(i) != ((int) serverString
                        .charAt(i) + 32))
                    return false;
            } else if (clientString.charAt(i) >= 'a'
                    && clientString.charAt(i) <= 'z') {
                if ((int) clientString.charAt(i) != ((int) serverString
                        .charAt(i) - 32))
                    return false;
            }
        }
    return true;
}
johnchen902
  • 9,531
  • 1
  • 27
  • 69
user2328999
  • 91
  • 3
  • 12
  • Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow! Also note there is never a need for more then one blank line of white space. – Andrew Thompson May 01 '13 at 13:51

2 Answers2

0

You could "invert" one string using: How can I invert the case of a String in Java? and then use Strings .equals method to compare them.

Method from How can I invert the case of a String in Java? included for completeness:

public static String reverseCase(String text)
{
    char[] chars = text.toCharArray();
    for (int i = 0; i < chars.length; i++)
    {
        char c = chars[i];
        if (Character.isUpperCase(c))
        {
            chars[i] = Character.toLowerCase(c);
        }
        else if (Character.isLowerCase(c))
        {
            chars[i] = Character.toUpperCase(c);
        }
    }
    return new String(chars);
}
Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
0

You've switched the + 32 and the - 32.

By the way, it's a lot easier to use methods like:

Character.isDigit
Character.isLowerCase
Character.isUpperCase
Character.toLowerCase
Character.toUpperCase
Lone nebula
  • 4,768
  • 2
  • 16
  • 16