In: Computer Science
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.
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)); } }