Question

I need help with my Java code. A user enters a sentence and the program will...

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<words.length; i++)
          {
              if(!s.add(words[i]))
              {
          duplicatewords.add(words[i]);
          count++;
    }
          }
        
          //display # of duplicates
          System.out.println("\nThe number of duplicates "
          + "in the sentance: " + count);
        
          //return duplicate words
          return duplicatewords;
    }

  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

replace if(!s.add(words[i])) with if(!s.add(words[i].toLowerCase())).

This will make every word to stored with lower case even if it was capitalized also it willl count as duplicate

Add a comment
Know the answer?
Add Answer to:
I need help with my Java code. A user enters a sentence and the program will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • I need help on creating a while loop for my Java assignment. I am tasked to...

    I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...

  • 1. Write a Java program to count all words in a string. User inputs a string...

    1. Write a Java program to count all words in a string. User inputs a string sentence and your program should count the number of words in that string. Test Data: Input the string: The quick brown fox jumps over the lazy dog. Expected Output: Number of words in the string: 9 2. Write a class called ATMTransaction. This class has a variable balance and account number. It has three methods, checkBalance, deposit and withdraws along with constructors getters and...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • I need Help PLZ ( Java Code ). Today 14.06.2019 I have to hand in my...

    I need Help PLZ ( Java Code ). Today 14.06.2019 I have to hand in my homework. reachability Actually, you find flying very good, but you do not trust the whole new-fangled flying stuff and the infrastructure it has built up. As a diehard medieval metal fan you prefer to travel from A to B but rather the good old catapult. Since one can not easily take the favorite cat on vacation with it (cats do not get drafts, which...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • Hey guys! Huge part of my grade !! My code works already but I need it...

    Hey guys! Huge part of my grade !! My code works already but I need it so it references to my node list class instead of my array, what would I need to change? My assignment  was to create a list from an assignment when we used an array, but I cant make it to work without the array, help! The professor said it was okay to either keep the array or to delete it altogether. package zipcode; import java.io.File; import...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT