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
7 Antworten
+ 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++;
}
}
+ 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.
+ 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.
+ 1
You're welcome...
0
Jayakrishna🇮🇳 is my edited code correct? I added the line you asked to.
0
Jayakrishna🇮🇳 how to apply it?
I incremented i inside the if condition but no change.
I edited the code also.
0
Jayakrishna🇮🇳 okayyyy it finally workedddd thank youuu again