How can i print result outside for loop ? What should i change in the code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How can i print result outside for loop ? What should i change in the code?

public static void main(String[] args) { String str ="muslim,pro,is,recognized,by,millions,of,islam,followers,around,the,world "; String[]ch=str.split(","); StringBuilder z =new StringBuilder(); for(String sstr : ch){ String c = sstr.substring(0,1).toUpperCase()+sstr.substring(1)+sstr.substring(sstr.length()-1).toUpperCase(); z.append(c); } System.out.print(z); } }

31st Jul 2022, 9:00 PM
Fatma hgazy
Fatma hgazy - avatar
14 Answers
+ 3
Fatma hgazy append string in StringBuilder with space int l = sstr.length(); String c = sstr.substring(0, 1).toUpperCase() + sstr.substring(1, l - 1) + sstr.substring(l - 1).toUpperCase(); z.append(c + " "); Then print z outside the loop
1st Aug 2022, 4:00 AM
A͢J
A͢J - avatar
+ 2
c = .. follow your code
31st Jul 2022, 10:55 PM
zemiak
+ 1
It seems that you are overwriting z in every iteration. You need to declare an ArrayList of strings to store the words in every iteration.
31st Jul 2022, 9:40 PM
Jan
Jan - avatar
+ 1
Declare this ArrayList outside the for loop. ArrayList<String> words = new ArrayList<String>(); Then add this at the end of the for loop words.add(c);
31st Jul 2022, 9:56 PM
Jan
Jan - avatar
+ 1
You don't need the StringBuilder at all.
31st Jul 2022, 9:58 PM
Jan
Jan - avatar
+ 1
public class Program { public static void main(String[] args) { String str ="muslim,pro,is,recognized,by,millions,of,islam,followers,around,the,world "; String[]ch=str.split(","); StringBuilder z =new StringBuilder(); for(String sstr : ch) { String c = sstr.substring(0,1).toUpperCase()+sstr.substring(1)+sstr.substring(sstr.length()-1).toUpperCase(); z.append(c); } System.out.print(z); } }
31st Jul 2022, 11:06 PM
zemiak
0
You should declare StringBuilder z outside the for loop and just write z = new StringBuilder() inside the for loop.
31st Jul 2022, 9:31 PM
Jan
Jan - avatar
0
Doesn't work
31st Jul 2022, 9:35 PM
Fatma hgazy
Fatma hgazy - avatar
0
Can you write this 🥺
31st Jul 2022, 9:41 PM
Fatma hgazy
Fatma hgazy - avatar
0
variables declared inside block (like loop) doesn't exist outside block StringBuilder z = new StringBuilder(); for (String sstr : ch) { String c =
31st Jul 2022, 10:35 PM
zemiak
0
String c equal what
31st Jul 2022, 10:37 PM
Fatma hgazy
Fatma hgazy - avatar
0
Doesn't work
31st Jul 2022, 10:57 PM
Fatma hgazy
Fatma hgazy - avatar
0
I want output like this MusliM PrO IS RecognizeD BY MillionS OF IslaM FollowerS ArounD ThE WorlD Ok About space i will do like this at the end behind z like this System.out.print(z+ " "); But the problem now your code didn't output the same result
31st Jul 2022, 11:09 PM
Fatma hgazy
Fatma hgazy - avatar
0
add .append(" "); after each word. append is the method of StringBuilder. and sstr.substring(1) is wrong because you want to cut the last char btw for effective code use append() instead all string concatenation by + here //c = z.append(..).append(..).append(..) .. /first_char .middle .end_char
31st Jul 2022, 11:18 PM
zemiak