SlideShare a Scribd company logo
1 of 2
Download to read offline
Can someone please fix my code for a hashtable frequencey counter: Implement a Java program
that counts word frequencies in a text file. Use a hashtable to store the data the words are the
keys, and their frequencies are the values. The output of the program is the complete list of all
the words and their frequencies in descending order of frequencies when two words have the
same frequency, output them by alphabetical order. Each line of output consists of a word, a tab,
and the frequency. A sample input is "The Tragedy of Hamlet, Prince of Denmark", in the file
Hamlet When processing the text, you should keep just the words, and discard all punctuation
marks, digits, and so on. You also need to turn all upper case letters to lower cases.
Solution
Your code is fine except for apostrophe was also being replacced by space because you were
replaceing everything other than alphabets with space. I put apostrophe in exclusion list and then
processed that seperately. Check the code below
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.ArrayList;
/**
* Created by abdul on 2/6/2017.
*/
public class WordCounter {
public static String finalWord(String str) {
String processedWord = str.replaceAll("[^a-zA-Z']", " ").toLowerCase();
processedWord = processedWord.replaceAll("[']", ""); // replace ' with empty string
return processedWord;
}
private Hashtable hash = new Hashtable();
public void fileInput() throws FileNotFoundException {
File text = new File("E:/Shake.txt");
String word;
int count = 1;
Scanner in = new Scanner(text);
ArrayList list = new ArrayList<>();
while (in.hasNextLine()) {
String line = in.nextLine();
line = finalWord(line);
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens()) {
word = st.nextToken();
if (hash.containsKey(word)) {
hash.put(word, hash.get(word) + 1);
//int count = (Integer) hash.get(word);
//hash.put(word, count + 1);
count++;
} else {
hash.put(word, 1);
count = 1;
}
}
}
Map map = new TreeMap(hash);
//System.out.println(map);
Set set = map.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + " ");
System.out.println(me.getValue());
}
}
public static void main (String[]args) throws FileNotFoundException {
WordCounter abc = new WordCounter();
abc.fileInput();
}
}

More Related Content

Similar to Can someone please fix my code for a hashtable frequencey counter I.pdf

Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1Dr.Ravi
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20Max Kleiner
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashessana mateen
 
Perl programming language
Perl programming languagePerl programming language
Perl programming languageElie Obeid
 
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdfHey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdfrishabjain5053
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl coursepartiernlow
 
Php basics
Php basicsPhp basics
Php basicshamfu
 
Hello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdfHello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdfnamarta88
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionProf. Wim Van Criekinge
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Andrea Telatin
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressionsKrishna Nanda
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsRoy Zimmer
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
 

Similar to Can someone please fix my code for a hashtable frequencey counter I.pdf (20)

Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Ch09
Ch09Ch09
Ch09
 
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdfHey, I need some help coding this C++ assignment.ObjectivesThis.pdf
Hey, I need some help coding this C++ assignment.ObjectivesThis.pdf
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl courseparti
 
Javascript
JavascriptJavascript
Javascript
 
Hashes Master
Hashes MasterHashes Master
Hashes Master
 
Php basics
Php basicsPhp basics
Php basics
 
Hello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdfHello, I need help with the following assignmentThis assignment w.pdf
Hello, I need help with the following assignmentThis assignment w.pdf
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
 
Python regular expressions
Python regular expressionsPython regular expressions
Python regular expressions
 
Java Script Introduction
Java Script IntroductionJava Script Introduction
Java Script Introduction
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
 

More from anjandavid

I am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdfI am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdfanjandavid
 
How many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdfHow many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdfanjandavid
 
How are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdfHow are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdfanjandavid
 
Hi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdfHi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdfanjandavid
 
How did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdfHow did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdfanjandavid
 
For problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdfFor problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdfanjandavid
 
Give an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdfGive an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdfanjandavid
 
Discuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdfDiscuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdfanjandavid
 
E, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdfE, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdfanjandavid
 
Describe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdfDescribe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdfanjandavid
 
Briefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdfBriefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdfanjandavid
 
A student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdfA student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdfanjandavid
 
All answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdfAll answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdfanjandavid
 
A polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdfA polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdfanjandavid
 
Why are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdfWhy are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdfanjandavid
 
Which of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdfWhich of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdfanjandavid
 
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdfWhich liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdfanjandavid
 
What is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdfWhat is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdfanjandavid
 
What are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdfWhat are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdfanjandavid
 
What are the benefits of using the Theory X and Theory Y management .pdf
What are the benefits of using the Theory X and Theory Y management .pdfWhat are the benefits of using the Theory X and Theory Y management .pdf
What are the benefits of using the Theory X and Theory Y management .pdfanjandavid
 

More from anjandavid (20)

I am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdfI am exploring the basic components of advanced broadband networks..pdf
I am exploring the basic components of advanced broadband networks..pdf
 
How many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdfHow many different instances of the ls command are installed on .pdf
How many different instances of the ls command are installed on .pdf
 
How are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdfHow are the Allen Bradley SLC 500 program files organizedSolutio.pdf
How are the Allen Bradley SLC 500 program files organizedSolutio.pdf
 
Hi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdfHi, I need help please, this is about KaseyaWich o the following .pdf
Hi, I need help please, this is about KaseyaWich o the following .pdf
 
How did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdfHow did WWI and its aftermath provide African Americans with opportu.pdf
How did WWI and its aftermath provide African Americans with opportu.pdf
 
For problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdfFor problems 3 and 4, consider the following functions that implemen.pdf
For problems 3 and 4, consider the following functions that implemen.pdf
 
Give an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdfGive an example of a system. What are the Components, Attributes,.pdf
Give an example of a system. What are the Components, Attributes,.pdf
 
Discuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdfDiscuss the attribution process and attribution errorsSolution.pdf
Discuss the attribution process and attribution errorsSolution.pdf
 
E, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdfE, an individual, received $40,000 of non-eligible dividends from Ca.pdf
E, an individual, received $40,000 of non-eligible dividends from Ca.pdf
 
Describe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdfDescribe one (1) example in which laws granting freedom of the press.pdf
Describe one (1) example in which laws granting freedom of the press.pdf
 
Briefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdfBriefly discuss 3–5 key trends in the modern health care operation.pdf
Briefly discuss 3–5 key trends in the modern health care operation.pdf
 
A student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdfA student obtained the following data Mass of water in calorimeter 3.pdf
A student obtained the following data Mass of water in calorimeter 3.pdf
 
All answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdfAll answers must be in your own words.What is importance of the Wa.pdf
All answers must be in your own words.What is importance of the Wa.pdf
 
A polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdfA polycationic mRNA contains two or more promoter sequences. True Fal.pdf
A polycationic mRNA contains two or more promoter sequences. True Fal.pdf
 
Why are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdfWhy are electrons shared in molecular compoundsWhy are electron.pdf
Why are electrons shared in molecular compoundsWhy are electron.pdf
 
Which of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdfWhich of these isare true of UDPa. It provides reliability, flow-c.pdf
Which of these isare true of UDPa. It provides reliability, flow-c.pdf
 
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdfWhich liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
Which liquid would BaCl Which liquid would BaCl 4. Which liquid.pdf
 
What is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdfWhat is employee involvement What are some of the benefits of invol.pdf
What is employee involvement What are some of the benefits of invol.pdf
 
What are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdfWhat are the pros and cons of technological leader versus technologi.pdf
What are the pros and cons of technological leader versus technologi.pdf
 
What are the benefits of using the Theory X and Theory Y management .pdf
What are the benefits of using the Theory X and Theory Y management .pdfWhat are the benefits of using the Theory X and Theory Y management .pdf
What are the benefits of using the Theory X and Theory Y management .pdf
 

Recently uploaded

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Recently uploaded (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

Can someone please fix my code for a hashtable frequencey counter I.pdf

  • 1. Can someone please fix my code for a hashtable frequencey counter: Implement a Java program that counts word frequencies in a text file. Use a hashtable to store the data the words are the keys, and their frequencies are the values. The output of the program is the complete list of all the words and their frequencies in descending order of frequencies when two words have the same frequency, output them by alphabetical order. Each line of output consists of a word, a tab, and the frequency. A sample input is "The Tragedy of Hamlet, Prince of Denmark", in the file Hamlet When processing the text, you should keep just the words, and discard all punctuation marks, digits, and so on. You also need to turn all upper case letters to lower cases. Solution Your code is fine except for apostrophe was also being replacced by space because you were replaceing everything other than alphabets with space. I put apostrophe in exclusion list and then processed that seperately. Check the code below import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.ArrayList; /** * Created by abdul on 2/6/2017. */ public class WordCounter { public static String finalWord(String str) { String processedWord = str.replaceAll("[^a-zA-Z']", " ").toLowerCase(); processedWord = processedWord.replaceAll("[']", ""); // replace ' with empty string return processedWord; } private Hashtable hash = new Hashtable(); public void fileInput() throws FileNotFoundException { File text = new File("E:/Shake.txt"); String word; int count = 1; Scanner in = new Scanner(text); ArrayList list = new ArrayList<>(); while (in.hasNextLine()) {
  • 2. String line = in.nextLine(); line = finalWord(line); StringTokenizer st = new StringTokenizer(line); while (st.hasMoreTokens()) { word = st.nextToken(); if (hash.containsKey(word)) { hash.put(word, hash.get(word) + 1); //int count = (Integer) hash.get(word); //hash.put(word, count + 1); count++; } else { hash.put(word, 1); count = 1; } } } Map map = new TreeMap(hash); //System.out.println(map); Set set = map.entrySet(); Iterator i = set.iterator(); while(i.hasNext()){ Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + " "); System.out.println(me.getValue()); } } public static void main (String[]args) throws FileNotFoundException { WordCounter abc = new WordCounter(); abc.fileInput(); } }