public void actionPerformed(ActionEvent e) { String a; int i; a=str.getText(); for (i = 0; i<a.length();i++) { if (Character.isLetter(a.charAt(i))){ if (Character.isLowerCase(a.charAt(i))) a.toUpperCase(); else a.toLowerCase(); } } res.setText(a); } 
  • Guys, I just started teaching ... I can't figure out what I did wrong, help either with a website, where you can read this info, or with a piece of code. thanks in advance - Indentikon
  • one
    What exactly is the problem? - etki
  • @Etki essence of taka - I have buttons, mold (I did not throw all the code). I need to enter the text for example "AAAaA" - press the result button and it should give me "aaAAa" and it gives the same text "AAAaA". - Indentikon

3 answers 3

I am not a javist, but nevertheless, toUpperCase and toLowerCase do not change the string itself, but return a new, modified, something like:

 public void actionPerformed(ActionEvent e) { String a; String b = ""; int i; a=str.getText(); for (i = 0; i<a.length();i++) { if (Character.isLetter(a.charAt(i))){ if (Character.isLowerCase(a.charAt(i))) b += Character.toUpperCase(a.charAt(i)); else b += Character.toLowerCase(a.charAt(i)); } } res.setText(b); } 
  • and change it for me as then? - Indentikon
  • added sample code - vik_78
  • when I enter "aAA" it gives the result "AAAaaaaaA" and when I enter just "a" it gives the result "A" - it is true. What's the problem? why is it his troit that - Indentikon
  • one
    @ vik_78 You change the case of the entire string, rather than a specific letter, and repeatedly plus the entire string. - Igor
  • 2Igor, yes, that's right, after Kamenta noticed that the whole line is used - vik_78
  • We declare an array of characters, the length of the number of characters in the string.
  • In the loop reverse symbol put into it.
  • At the end of the array of chars make the line.
 String a = "aaAAa"; char[] chars = a.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); } } System.out.println(new String(chars)); 

Test: http://ideone.com/YBq5br

  • there I need to display in the form itself and not in the console. The above man did. but thanks! - Indentikon
  • four
    @ Indentikon as well and what's the problem replace System.out.println(new String(chars)); on res.setText(new String(chars)); ? or life without copy-paste is not life? ....... you still say that you are not able to String a = "aaAAa"; replace with String a; a=str.getText(); String a; a=str.getText(); - Alexey Shimansky

Not the most optimal and good option, but still:

 String content = "HelloWorld"; String result = content .chars() .map(s -> Character.isUpperCase(s) ? Character.toLowerCase(s) : Character.toUpperCase(s)) .mapToObj(s -> (char) s) .map(Object::toString) .collect(joining());