Reputation: 17
I need to use the IndexOf(); method to store the different indexes in a word. For example, say the word is "giraffe" I want to make a code that stores indexes in a variable so it looks like the following:
input: System.out.println(indexes);
output: 4 5
the problem is that using a while loop executes the program and just displays the numbers. I want to make indexes a string by using the following format:
String indexes = indexA + " " + indexB
where indexA would be the first location and indexB would be the next. If there was more it would just keep creating new indexes (indexC, etc.) and keep adding them with a format that places a space in between them.
If there is an easier method to use to do this that would be helpful too.
Upvotes: 0
Views: 226
Reputation: 2900
It sounds like you have figured out how to gather the indices you want, and just need to format the output.
You can add all the indices to an array, then use Apache Commons's StringUtils
:
Integer[] indices = new Integer[]{1, 2, 3};
String output = StringUtils.join(indices, " ");
System.out.println(output);
prints
1 2 3
Upvotes: 2