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
I'm trying to find a code to check the occurrences of the word or string I...
I'm trying to find a code to check the occurrences of the word or string I wrote my own code but it's not working can you please help and fix my code #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1;...
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...
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...
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)...
[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...
i am trying to figure out how to find class boundaries
i am trying to figure out how to find class boundaries
i am trying to wrire a word and change it to capital letters using client-server. im...
i am trying to wrire a word and change it to capital letters using client-server. im not able to write any words when i run the file in netbeans or command promot. is it something with my code? /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package assignment3retake; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter;...
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...
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[]...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT