+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

4/20/2019 4:44:27 PM

Hershewajue

8 Answers

New Answer

+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.

+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'. ;-)

+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!

+6

HonFu, thanks for the heads up! I’ll be more careful next time.

+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!

+5

Decimis † 𝕯𝖊𝖈𝖎𝖒𝖎𝖘 Thanks for the advice

+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.

+3

Thanks i did it... I removed the System.out.println(figures); out of the loop... Thanks guys, you really made my day.