Question

In: Computer Science

Create a static method with the appropriate inputs and outputs. Call each of them in the...

Create a static method with the appropriate inputs and outputs. Call each of them in the main method.

Write a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each word in the input string is separated by whitespace.3 2Use the equals() method. 3The split() method of String can split an input up along a provided special string called a regular expression or regex. A regex is much like a code for pattern, and split() will match anything in that pattern. The regex "\\s+" matches any amount of whitespace. 3 If our input String is "Hello, world!", then the output should be ["Hello,", "world!"]. For extra credit, sanitize the String, cleaning it up so that we remove the punctuation and other extraneous characters such that the output in the above example would become ["Hello", "world"] This method returns List.

Please Do this in Java.

Solutions

Expert Solution

Thanks for the question.

Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

Thanks
===========================================================================
import java.util.Arrays;

public class Tokenizer {


    public static String[] stringToListOfWords(String sentence) {

        // split the sentence using space, split() returns the tokens as String arrays
        String[] words = sentence.split("\\s+");

        for (int i = 0; i < words.length; i++) {
            // iteerate over each word in the array
            // using replaceAll() method replaces all punctuations from the word with ""
            words[i] = words[i].replaceAll("[^a-zA-Z ]", "");
        }
        // return the array back
        return words;

    }

    public static void main(String[] args) {

        String sentence = "Hello, world!";
        String[] words = stringToListOfWords(sentence);
       // print the array of words
        System.out.println(Arrays.toString(words));
    }
}


Related Solutions

Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called removeAllInstances() which takes in a List and item4 . The method then proceeds to remove each item in the list that matches the given item. For example, if the method is passed the List [1, 4, 5, 6, 5, 5, 2] and the Integer 5, the method removes all 5’s from the List. The List then becomes [1,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called isPermutaion() which takes in two List objects which contain the same types. It returns true if the Lists are permutations of each other and false if not. Two lists are permutations if they contain the same elements, including the same number of duplicates, but they don’t have to contain the elements in the same order. For example, [1,2,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method named allMultiples() which takes in a List of integers and an int. The method returns a new List of integers which contains all of the numbers from the input list which are multiples of the given int. For example, if the List is [1, 25, 2, 5, 30, 19, 57, 2, 25] and 5 was provided, the new list...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called unique() which takes in a List and returns true if all the items in the List are unique. All the items are unique if none of them are the same.Return false otherwise. Please do this in Java.
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist()....
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist(). • Norm.Inv(). • T.Dist().
Objectives • Use the static methods and static fields. •Use the method-call/return mechanism. • Use the...
Objectives • Use the static methods and static fields. •Use the method-call/return mechanism. • Use the random-number generation. • Overload methods. Problem Specification Write a program that lets a user play "Rock, Paper, Scissors" against the computer or computer against computer. In user-computer game, the program should ask the user to choose one of the three choices, and then the computer randomly picks one (without knowing what the user has chosen). For this problem, the user should be asked to...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
Using what you know about how to take inputs and make outputs to the console, create...
Using what you know about how to take inputs and make outputs to the console, create a check printing application. Your application will read in from the user an employee's name and salary, and print out a Console Check similar to the following. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> > | $1,000,000 | > > > > ___Pay to the Order of___ Johnny PayCheck > >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Please only use technique from chapter 1 through 3 in C++ from control structure through object 9th edition
Create a detailed process flowchart that includes process inputs, process outputs, activities, activity times, processing and...
Create a detailed process flowchart that includes process inputs, process outputs, activities, activity times, processing and labor requirements for a fictional retail chain of coffee shops. Capacity analysis of the process, including the strategies used to address expected customer demand. Suggested metrics used to measure the process performance. Identify the process problem and make suggestions on how the process can be improved
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT