Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program to parse words from a String. Do not use toeknizer classes.

ID: 3723118 • Letter: W

Question

Write a Java program to parse words from a String. Do not use toeknizer classes. The one java class must be called SentParser Your class should be able to break the given sentence into words, where word is defined as the characters between any spaces, commas, or periods. The class should have a constructor that takes a sentence as a parameter. The class should define several public value methods. The class should have one method called "public static void main ()" that demonstrates what your class can do with the specific hard-coded sentence shown below. It should write this text to the console:

Sentence:

The quick brown fox, jumped over the lazy dog.

Number of words: 9

Number of unique words: 9

Explanation / Answer

import java.util.*;

public class SentParser{

String sentence;

public SentParser(String sentence) {

super();

this.sentence = sentence;

}

public void getWords(){

String[] words = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\s+");

System.out.println("Number of words: " + words.length);

}

public void getUniqueWords(){

String[] words = sentence.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\s+");

Set<String> uniqueWords = new HashSet<String>();

for (String word : words)

uniqueWords.add(word);

System.out.println("Number of unique words: "+ uniqueWords.size());

}

public static void main(String args[]){

Scanner in = new Scanner(System.in);

System.out.println("Sentence: ");

String sentence = in.nextLine();

SentParser sp = new SentParser(sentence);

sp.getWords();

sp.getUniqueWords();

}

}

**Comment for any further queries.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote