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)); } } res.setText(b); } 

It is necessary to make the first half of the line in capital letters. It does everything for me. How to split a string? (An example should look like this: for example, enter "aaaaaaaa" should convert to "AAAAaaaa" - or "aaaa" in "AAAaa"

  • tried i < a.length() / 2 ? - Arnial

3 answers 3

You seem to have forgotten to add capital letters already.

 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)) && a.length()/2 > i ) b += Character.toUpperCase(a.charAt(i)); else b += a.charAt(i); } } res.setText(b); } 
  • For some reason, it does not work as it should. - Indenticon
  • Not so set the condition, corrected - Artem Gorlachev
  • Golachev works. Thanks, bailed out! - Indenticon

You run over the entire line in a loop condition. You only need half of it. So it is necessary to finish the cycle on half of the line, specifying in the condition only half the length of the line

 for (i = 0; i < a.length() / 2; i++) 
  • Everything is good, but it cuts off the second part, for example: "aaaaaa" the answer is "AAA", but it is necessary that it be "AAAaaa" - Indenticon

Run to half a line, i.e. a.length()/2

  • @ Geverg Harutyunyan Everything is fine, but it cuts off the second part, for example: "aaaaaa" the answer is "AAA", but it is necessary that it be "AAAaaa" - Indenticon