Use a ListIterator, so you can replace the value while iterating:
for (ListIterator<String> iter = story.listIterator(); iter.hasNext(); ) {
String line = iter.next();
line = line.replace("[Color]", "BLUE");
iter.set(line);
}
story.forEach(System.out::println);
Output
Select the type of bread you want to use. Many prefer the taste of BLUE bread, while others prefer [Noun1] bread because it is healthy.
Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.
Choose the type of peanut butter - either [Adjective1] or [Adjective2].
Take out [Number] slice(s) of bread.
Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.
Now [Verb2] the peanut butter on the other piece of bread.
Put them together, and you have a PB&J [Verb3].
If you want to replace many of the placeholders in a single iteration, then do it like this (Java 9+):
Map<String, String> map = new HashMap<>();
map.put("Color", "BLUE");
map.put("Adjective1", "SMOOTH");
map.put("Adjective2", "CHUNKY");
map.put("Number", "THREE");
Pattern p = Pattern.compile("\\[([^\\]]+)\\]");
for (ListIterator<String> iter = story.listIterator(); iter.hasNext(); ) {
String line = iter.next();
line = p.matcher(line).replaceAll(mr -> map.getOrDefault(mr.group(1), mr.group(0)));
iter.set(line);
}
Output
Select the type of bread you want to use. Many prefer the taste of BLUE bread, while others prefer [Noun1] bread because it is healthy.
Choose the flavor of Jam/Jelly. I personally prefer [Food] jam, but you can use whatever you want.
Choose the type of peanut butter - either SMOOTH or CHUNKY.
Take out THREE slice(s) of bread.
Use a [Noun2] to [Verb1] the jam all over on of the pieces of bread.
Now [Verb2] the peanut butter on the other piece of bread.
Put them together, and you have a PB&J [Verb3].