how to convert Lower case letters to upper case letters & and upper case letters to lower case letters

setText is changing the text content to exactly what you give it, not appending it.

Convert the String from the field first, then apply it directly...

String value = "This Is A Test";
StringBuilder sb = new StringBuilder(value);
for (int index = 0; index < sb.length(); index++) {
    char c = sb.charAt(index);
    if (Character.isLowerCase(c)) {
        sb.setCharAt(index, Character.toUpperCase(c));
    } else {
        sb.setCharAt(index, Character.toLowerCase(c));
    }
}

SecondTextField.setText(sb.toString());

You don't have to track whether you've already changed the character from upper to lower. Your code is already doing that since it's basically:

1   for each character x:
2       if x is uppercase:
3           convert x to lowercase
4       else:
5           if x is lowercase:
6                convert x to uppercase.

The fact that you have that else in there (on line 4) means that a character that was initially uppercase will never be checked in the second if statement (on line 5).

Example, start with A. Because that's uppercase, it will be converted to lowercase on line 3 and then you'll go back up to line 1 for the next character.

If you start with z, the if on line 2 will send you directly to line 5 where it will be converted to uppercase. Anything that's neither upper nor lowercase will fail both if statements and therefore remain untouched.


You can use StringUtils.swapCase() from org.apache.commons