Question

I am trying to figure out how to find the most common word length in the...

I am trying to figure out how to find the most common word length in the british dictionary. I have inputted the text file for the british dictionary into my program. I tried a tedious way, but it doesn't seem effective. Can someone help me?

String longestWord = "";

       int maxLength =0;

       int count1 =0, count2 =0, count3=0, count4=0, count5=0, count6=0, count7=0, count8=0, count9=0, count10=0;

       Scanner scanFile = new Scanner (new File("BritishDictionary.txt"));

       ArrayList<String> book = new ArrayList<String>();

       while(scanFile.hasNext())

       {

           book.add(scanFile.next());

          

           for(int i =0; i<= book.size()-1;i++)

           {

               if(book.get(i).length()>longestWord.length())

               {

                   maxLength = book.get(i).length();

                   longestWord = book.get(i);

                  

               }

              

           }

           for(int i =0; i<= book.size()-1; i++)

           {

               if(book.get(i).length() == 2)

               {

                   count1++;

               }

               if(book.get(i).length()==3)

               {

                   count2++;

               }

               if(book.get(i).length()==4)

               {

                   count3++;

               }

               if(book.get(i).length() == 5)

               {

                   count4++;

               }

               if(book.get(i).length() == 6)

               {

                   count5++;

               }

               if(book.get(i).length() ==7)

               {

                   count6++;

               }

               if(book.get(i).length() ==8)

               {

                   count7++;

               }

               if(book.get(i).length()==9)

               {

                   count8++;

               }

               if(book.get(i).length() ==10)

               {

                   count9++;

               }

               if(book.get(i).length()==11)

               {

                   count10++;

               }

           }

       }

       scanFile.close();

       System.out.println("The longest word is:" + longestWord);

      

Homework Answers

Answer #1

Hi buddy, please find the below java program

import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // Scanner object to read the Input file
       Scanner scanFile = new Scanner (new File("BritishDictionary.txt"));
       //HashMap to store count of strings of certain lengths
HashMap<Integer,Integer> hm = new HashMap();
int wordLength=0,maxCount = 0;
while(scanFile.hasNext()){
String str = scanFile.next();
int l = str.length();
if(!hm.containsKey(l)){
hm.put(l,0);
}
//Add the length to the map
hm.put(l,hm.get(l)+1);
}
Set<Integer> key = hm.keySet();
//Iterate through all the elements in the map and find the element
//With most number of occurances
for(Integer x : key){
if(hm.get(x)>maxCount){
maxCount = hm.get(x);
wordLength = x;
}
}
System.out.println("The most common word length is:" + wordLength);
   }
}

INPUT :

a a a adf afd
fd a dfsd adf df df f adf bdf cdf edf

OUTPUT :

The most common word length is:3

Know the answer?
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
Similar Questions
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
I am trying to take a string of numbers seperated by a single space and covert...
I am trying to take a string of numbers seperated by a single space and covert them into a string array. I have created the following code but it only works if the numbers are seperated a a comma or something similar to that. Example of what I am trying to achieve: string input = "1 1 1 1 1" turn it into.... int[] x = {1,1,1,1} so that it is printed as... [1, 1, 1, 1]    This is...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
i am trying to figure out how to find class boundaries
i am trying to figure out how to find class boundaries
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
C Programming I am trying to also print the frequency and the occurrence of an input...
C Programming I am trying to also print the frequency and the occurrence of an input text file. I got the occurrence to but cant get the frequency. Formula for frequency is "Occurrence / total input count", this is the percentage of occurrence. Can someone please help me to get the frequency to work. Code: int freq[26] = {0}; fgets(input1, 10000, (FILE*)MyFile); for(i=0; i< strlen(input); i++) { freq[input[i]-'a']++; count++; } printf("Text count = %d", count); printf("\n"); printf("Frequency of plain text\n");...
Question: I am using three different ways to execute this program. I am a bit confused...
Question: I am using three different ways to execute this program. I am a bit confused on the time complexity for each one. Can someone explain the time complexity for each function in relation to the nanoseconds. import java.util.HashMap; import java.util.Map; public class twosums {       public static void main(String args[]) {               int[] numbers = new int[] {2,3,9,4,8};;        int target = 10;               long nano_begin1 = System.nanoTime();        int[]...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of...
Hi, I am trying to create an XML JTree viewer using the DOM parser instead of the SAX parser, I am having trouble changing my code in order to utilize the DOM parser to extract the XML data into the tree structure. Would you be able to explain what changes should be made to the code in order to use the DOM parser instead of the SAX parser? // Java Packages //      import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import...
Having trouble with this lab. This is the table. I am trying to figure out the...
Having trouble with this lab. This is the table. I am trying to figure out the final reaction rate. Can someone please do one and give me a step by step so I can do the rest. Data Table 1: Varying the Concentration of 1.0 M HCL ---- C o n c e n t r a t i o n s ---- # drops # drops # drops Initial Initial Final Final Reaction Time (sec) Reaction Well # HCl...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT