public class MultiSplit { // TODO - write your code below this comment. // You w
ID: 3915802 • Letter: P
Question
public class MultiSplit { // TODO - write your code below this comment. // You will need to write two methods: // // 1.) A method named multiSplit which will take an // array of strings, as well as a String specifying // the regular expression to split on. // The method returns a two-dimensional array, where each // element in the outer dimension corresponds to one // of the input Strings. For example: // // multiSplit(new String[]{"one,two", "three,four,five"}, ",") // // ....returns... // // { { "one", "two" }, { "three", "four", "five" } } // // 2.) A method named printSplits which will take the sort // of splits produced by multiSplit, and print them out. // Given the example above, this should produce the following // output: // // 0: one two // 1: three four five // // Each line is permitted to have a trailing space (" "), which // should make your implementation simpler. // // DO NOT MODIFY main! public static void main(String[] args) { printSplits(multiSplit(args, ",")); } }
Explanation / Answer
public static String[][] multiSplit(String[] a, String delimiter)
{
String[][] result = new String[a.length][];
// looping through each string in a
for(int i=0; i<a.length; i++)
{
// getting the separated strings by delimeter, to the row in the result array
result[i] = a[i].split(delimiter);
}
return result;
}
public static void printSplits(String[][] a)
{
// LOOPING through each array in a
for(int i=0; i<a.length; i++)
{
// printing each string in that array
System.out.printf("%d: ", i);
for(int j=0; j<a[i].length; j++)
{
System.out.printf("%s ", a[i][j]);
}
System.out.println();
}
}
// DO NOT MODIFY main!
public static void main(String[] args) {
printSplits(multiSplit(args, ","));
}
}
/*SAMPLE OUTPUT
0: one two
1: three four five
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.