In: Computer Science
Step 3: Edit MultiSplit.java
Download the MultiSplit.java file, and open it in jGrasp (or a text editor of your choice). You will need to write a method that will take an array of Strings (String[]) along with a regular expression, and will call split() on each one of those Strings with the given regular expression. The result of this method should be a two-dimensional array of String (String[][]).
You will also need to write a method to print out this two-dimensional array of Strings.
The main method will call both methods, using “,” as the regular expression. Example output is shown below, which corresponds to the command-line arguments "one,two,three" " alpha,beta,gamma" "delta" "first,second":
0: one two three 1: alpha beta gamma 2: delta 3: first second
The output above shows that the 0th element ("one,two,three") produced the values one, two, and three when a split with “,” was performed. Each subsequent line follows the same general pattern for all the subsequent indices in the command-line argument array
-------------------------------------------------------------------------------------------------------------------------
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 (but not required) to have a trailing
// space (" "), which may make your implementation simpler.
//
// DO NOT MODIFY main!
public static void main(String[] args) {
String[][] splits = multiSplit(args, ",");
printSplits(splits);
}
}
import java.util.*;
public class MultiSplit{
public List mutliSplit(String arr[], String separator){
ArrayList<String[]> ret = new ArrayList<String[]>();
for (int i=0;i<arr.length;i++){
String [] separate = arr[i].split(separator);
ret.add(separate);
}
return ret;
}
public static void printSplits(ArrayList<String[]> ret){
int count = 0;
while (ret.size()>count){
String[] list = ret.get(count);
System.out.print(count+": ");
for (int i=0;i<list.length;i++)
System.out.print(list[i] + " ");
}
}
public static void main(String[] args) {
ArrayList<String[]> splits = multiSplit(args, ",");
printSplits(splits);
}
}
In this program, instead of using a 2d array, an ArrayList of 1d String arrays is taken. If THAT is a problem, please comment, and I'll post edit it for the same.