Question
I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many
I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many times. If the same word is in the sentence twice, but one is capitalized, it will not count it as a duplicate. How can I make the program read the same word as the same word, even if one is capitalized and the other is not?
For example, "the" and "The" should be counted as a duplicate word.
import java.util.HashSet; import java.util.Collections; import java.util.Set; import java.util.Scanner; public class DuplicateWords { public static void main(String[] args) { //input string // String input = "I am taking Advanced Java this spring semester " // + "I will be qualified ton graduate next fall semester."; Scanner in = new Scanner(System.in); System.out.print("Enter a sentence: "); String sentence = in.nextLine(); //call method duplicates using isentance as argument Set duplicatewords = duplicates(sentence); System.out.println("The duplicate words in the sentence: " + duplicatewords); } //define metiod duplicates to find duplicates in sentence public static Set duplicates(String sent) { //variabes int i=0, count=0; //use if loop to empty sgring if(sent == null || sent.isEmpty()) { return Collections.emptySet(); } //the fiunction for new set Set duplicatewords = new HashSet<>(); //use split function to check duplicates String[] words = sent.split("\\s+"); Set s = new HashSet<>(); //loop to find duplicates in sentence and # for(i=0; i }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started