Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students also viewed these Databases questions

Question

3. Contrast relational contexts in organizations

Answered: 1 week ago

Question

2. Describe ways in which organizational culture is communicated

Answered: 1 week ago

Question

1. Describe and compare approaches to managing an organization

Answered: 1 week ago