I have a task where I'm asked to create a method that merges multiple Strings so that the first character in the first string is followed by the first character in the 2nd String followed by the first character in the 3rd string and so on.
public static String merge(String... s)
if I would merge("AH", "HI", "U")
the result would be AHUHI. I'm just not quite sure how to deal with this problem when the amount of Strings are unknown, someone has any idea how this could be executed?¨
This is what I tried:
public static String merge(String... s)
{
StringBuilder b = new StringBuilder();
for(int i = 0; i < s.length ; i++)
{
for(int y = 0; y < s.length ; y++)
{
b.append(s[y].charAt(i));
}
}
return b.toString();
}
and this is the exception I got:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0