+ 2

Create an ArrayList of Strings and write a program that places a string of 4 asterisks"****"in front of every string of length 4

For example, suppose that a variable called list contains the following values: {"this", "is", "lots", "of", "fun", "for", "every", "Java", "programmer"} And after running the program the list should store the following values after the call: {"****", "this", "is", "****", "lots", "of", "fun", "for", "every", "****", "Java", "programmer"} Notice that you leave the original strings in the list, "this", "lots", "Java", but include the four asterisk string in front of each to mark it

20th Apr 2019, 4:44 PM
Hershewajue
Hershewajue - avatar
8 Réponses
+ 2
import java.util.*; import java.util.ArrayList; public class forLoop{ public static void main(String[]args){ ArrayList<String> figures = new ArrayList<String>(); figures.add("this"); figures.add("is"); figures.add("lots"); figures.add("of"); figures.add("fun"); figures.add("for"); figures.add("every"); figures.add("java"); figures.add("programmer"); for(int i = 0; i < figures.size(); ++i) { if(figures.get(i).length() == 4) { figures.add(i++, "****"); System.out.println(figures); } } } } /* Outputs: [****, this, is, lots, of, fun, for, every, java, programmer] [****, this, is, ****, lots, of, fun, for, every, java, programmer] [****, this, is, ****, lots, of, fun, for, every, ****, java, programmer] */ Thanks a lot for the tip. It worked. But my issue is that it shouldn't reiterate the result. What to do, just for it to display the last result.
20th Apr 2019, 9:50 PM
Hershewajue
Hershewajue - avatar
+ 7
Jack McCarthy, please consider that students of all ages from all over the world every day try to get their homework done by just posting it here. Please don't support this! Instead encourage them to do it themselves! Let them show their attempt, help them if necessary by giving little hints. Thousands of teachers all over the world and the education systems they work for will say 'thank you'. ;-)
20th Apr 2019, 5:07 PM
HonFu
HonFu - avatar
+ 6
Is this a challenge you are giving to us? If so: Not the right place. Q&A is about programming-related questions. Or do you have to solve this yourself? Then please show us your attempt!
20th Apr 2019, 5:00 PM
HonFu
HonFu - avatar
+ 6
HonFu, thanks for the heads up! I’ll be more careful next time.
20th Apr 2019, 5:10 PM
Jack McCarthy
Jack McCarthy - avatar
+ 5
Sure, here's a function that should do exactly what you're looking for: void placeAsterisks(ArrayList<String> list) { for(int i = 0; i < list.size(); ++i) { if(list.get(i).length() == 4) { list.add(i++, "****"); } } } You can call this function like so: placeAsterisks(list); Feel free to change the name of the function or whatever. Hope this helped!
20th Apr 2019, 5:02 PM
Jack McCarthy
Jack McCarthy - avatar
20th Apr 2019, 6:21 PM
Jack McCarthy
Jack McCarthy - avatar
+ 4
Jack McCarthy, Yeah HonFu is right. You have to be suspicious of questions like that here 😉 And if you are not aware, you can tag someone by starting with the @ symbol and then a list of the names of the people that commented on a question will show up and you can then select a name. If you tag someone somewhere else, I believe the list of names after the @ symbol will be the people that you follow.
20th Apr 2019, 6:19 PM
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘
Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 - avatar
+ 3
Thanks i did it... I removed the System.out.println(figures); out of the loop... Thanks guys, you really made my day.
20th Apr 2019, 9:59 PM
Hershewajue
Hershewajue - avatar