Question

In: Computer Science

Step 3: Edit MultiSplit.java Download the MultiSplit.java file, and open it in jGrasp (or a text...

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);
    }
}

Solutions

Expert Solution

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.


Related Solutions

Step 1: Edit SumMinMaxArgs.java Download the SumMinMaxArgs.java file, and open it in jGrasp (or a text...
Step 1: Edit SumMinMaxArgs.java Download the SumMinMaxArgs.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, parses them as ints, and then displays: The arithmetic sum of the arguments The smallest argument The largest argument If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line arguments 1 2 3 4 5 is shown...
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write: swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap. swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap. main contains some code which will call swapRows and swapCols, for the purpose of informal...
Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below: foo bar baz In the above example, three elements were taken. However, it's possible that we may request to take...
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AllEqual.java file, and open it in jGrasp (or a text editor of your choice). This program takes a number of command line arguments, converts them to integers, and then determines if they all have the same value. An example run of the program is below, using the command-line arguments 1 2 3: Are equal: false A further example is shown below, with the command-line arguments 2 2: Are equal: true In the event that the program is given...
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Compass.java file, and open it in jGrasp (or a text editor of your choice). This program will randomly print out a compass direction, given a seed value for produce a random number with java.util.Random. Compass directions are mapped to integers according to the following table: Compass Direction Integer ID North 0 Northeast 1 East 2 Southeast 3 South 4 Southwest 5 West 6 Northwest 7 Further details are in the comments of Compass.java. import java.util.Random; import java.util.Scanner; public...
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
Write a GUI-based program that allows the user to open, edit, and save text files. The...
Write a GUI-based program that allows the user to open, edit, and save text files. The GUI should include a labeled entry field for the filename and multi-line text widget for the text of the file. The user should be able to scroll through the text by manipulating a vertical scrollbar. Include command buttons labeled Open, Save, and New that allow the user to open, save and create new files. The New command should then clear the text widget and...
Python class Select a free ebook on gutenberg . org and download the plain text file...
Python class Select a free ebook on gutenberg . org and download the plain text file (utf8). the ebook is (The Devil's Dictionary by Ambrose Bierce) Write a program that does the following: Read in your ebook (any book, any language) Break the book into words (look at .split() for strings) Make a dictionary of all words and how often they occur: for instance if the word'the'happened 2000 time and the word'a' happened 1940 times word_dict= {'the' : 2000, 'a'...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT