0

Java ArrayList Beginner

I want to print 4 stars before every 4 letter word. What am I missing? https://code.sololearn.com/cr6SupTweTXp/?ref=app

26th Feb 2021, 10:32 AM
okurpants
okurpants - avatar
7 Respuestas
+ 1
okurpants you incrementing but before adding element so it adds after word not before. and using i<elements will skip added number of elements from last . This way works fine.. check now.. for (int i = 0; i < marks.size(); i++) { if (marks.get(i).length() == 4) { marks.add(i,"****"); if (i+1 !=elements) //to not to skip last element i++; } }
26th Feb 2021, 3:18 PM
Jayakrishna 🇮🇳
+ 1
okurpants marks.add(i,"****"); By this code, you are adding 4stars before a 1st 4char word. so then **** becomes your next (i+1) word. So again this above step gets repeated. so your checking length 4 for 1st 4length word by loop always. And if you use arraylist.length instead of elemenrs (size)., it will become infilte loop.. Whenever you add a 4star word ,increment i . (i++), (when if i+1 != elements ) so it will search next word instead of 4stars.
26th Feb 2021, 11:44 AM
Jayakrishna 🇮🇳
+ 1
No. okurpants Oh. Sry i mistakenly posted tested statement along with explanation.. i dont said to add It. Edited. Now again read full post (previous) to understand what is happening.. you can find solution i think.
26th Feb 2021, 2:41 PM
Jayakrishna 🇮🇳
+ 1
You're welcome...
26th Feb 2021, 3:40 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 is my edited code correct? I added the line you asked to.
26th Feb 2021, 2:22 PM
okurpants
okurpants - avatar
0
Jayakrishna🇮🇳 how to apply it? I incremented i inside the if condition but no change. I edited the code also.
26th Feb 2021, 3:03 PM
okurpants
okurpants - avatar
0
Jayakrishna🇮🇳 okayyyy it finally workedddd thank youuu again
26th Feb 2021, 3:37 PM
okurpants
okurpants - avatar