SlideShare a Scribd company logo
1 of 3
Download to read offline
JAVA
CountOccurrenceOfWords.java
Your program should take a text file name as a command line argument. It should read the file
and print the number of times the most-used word appears and, in ascending order, the words
that occur most frequently. Ignore case when processing the words in the file. Your program may
ignore punctuation. For example, if the file contained
The program's output should be
Words that appear 2 times: and are dachshunds dogs
Solution
//CountOccurrenceOfWords.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.*;
public class CountOccurrenceOfWords
{
public static void main(String[] file) throws FileNotFoundException, IOException
{
if (file.length == 0)
{
System.out.println("Usage: java CountOccurrenceOfWords targetfile");
System.exit(0);
}
BufferedReader br = null;
br = new BufferedReader(new FileReader(file[0]));
String text = null;
TreeMap treemap = new TreeMap();
try
{output
while ((text = br.readLine()) != null)
{
String[] line = text.split("[  t .,;:!?(){}]");
for (int count = 0; count < line.length; count++)
{
String key = line[count].toLowerCase();
if (key.length() > 0)
{
if (treemap.get(key) == null)
{
treemap.put(key, 1);
}
else
{
int value = treemap.get(key).intValue();
value++;
treemap.put(key, value);
}
}
}
}
Set> entrySet = treemap.entrySet();
int max = 0;
for (Map.Entry entry : entrySet)
{
if(entry.getValue() > max) max = entry.getValue();
}
// create an empty array list with an initial capacity
ArrayList maxFrequencyList = new ArrayList(100);
for (Map.Entry entry : entrySet)
{
if(max == entry.getValue()) maxFrequencyList.add(entry.getKey());
}
// sort maximum frequncy list
Collections.sort(maxFrequencyList);
System.out.print("Words that appear " + max + " times are: ");
for (String word : maxFrequencyList)
{
System.out.print(word + ", ");
}
System.out.println();
}
catch (IOException error)
{
System.out.println("Invalid File");
}
finally
{
br.close();
}
}
}
/*
output:
Words that appear 2 times are: and, are, dachshunds, dogs,
*/

More Related Content

Similar to JAVACountOccurrenceOfWords.javaYour program should take a text f.pdf

Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz SAurabh PRajapati
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMmanish kumar
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and filesMarcello Thiry
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in javaJayasankarPR2
 
asdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawasdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawWrushabhShirsat3
 
Write a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxWrite a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxlez31palka
 
Introduction
IntroductionIntroduction
Introductionrichsoden
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - JavaDaniel Ilunga
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docxtheodorelove43763
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java ProgrammingMath-Circle
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objectsvmadan89
 
Parsing food data lab - Please answer in JAVA Given a text file contai.pdf
Parsing food data lab - Please answer in JAVA Given a text file contai.pdfParsing food data lab - Please answer in JAVA Given a text file contai.pdf
Parsing food data lab - Please answer in JAVA Given a text file contai.pdfarcellzone
 
I would like this to be answered in java 9-8 LAB- Parsing food data.pdf
I would like this to be answered in java   9-8 LAB- Parsing food data.pdfI would like this to be answered in java   9-8 LAB- Parsing food data.pdf
I would like this to be answered in java 9-8 LAB- Parsing food data.pdfsojandas029
 
Eo gaddis java_chapter_02_5e
Eo gaddis java_chapter_02_5eEo gaddis java_chapter_02_5e
Eo gaddis java_chapter_02_5eGina Bullock
 
Perfomatix - Java Coding Standards
Perfomatix - Java Coding StandardsPerfomatix - Java Coding Standards
Perfomatix - Java Coding StandardsPerfomatix Solutions
 

Similar to JAVACountOccurrenceOfWords.javaYour program should take a text f.pdf (20)

Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 
Java file
Java fileJava file
Java file
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
 
Java basics
Java basicsJava basics
Java basics
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Introduction To Java.
Introduction To Java.Introduction To Java.
Introduction To Java.
 
asdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudawasdabuydvduyawdyuadauysdasuydyudayudayudaw
asdabuydvduyawdyuadauysdasuydyudayudayudaw
 
Write a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docxWrite a program that will count the number of characters- words- and l.docx
Write a program that will count the number of characters- words- and l.docx
 
Introduction
IntroductionIntroduction
Introduction
 
Object Oriented Programming - Java
Object Oriented Programming -  JavaObject Oriented Programming -  Java
Object Oriented Programming - Java
 
Description 1) Create a Lab2 folder for this project2.docx
Description       1)  Create a Lab2 folder for this project2.docxDescription       1)  Create a Lab2 folder for this project2.docx
Description 1) Create a Lab2 folder for this project2.docx
 
Basic Java Programming
Basic Java ProgrammingBasic Java Programming
Basic Java Programming
 
Classes and Objects
Classes and ObjectsClasses and Objects
Classes and Objects
 
Parsing food data lab - Please answer in JAVA Given a text file contai.pdf
Parsing food data lab - Please answer in JAVA Given a text file contai.pdfParsing food data lab - Please answer in JAVA Given a text file contai.pdf
Parsing food data lab - Please answer in JAVA Given a text file contai.pdf
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 
I would like this to be answered in java 9-8 LAB- Parsing food data.pdf
I would like this to be answered in java   9-8 LAB- Parsing food data.pdfI would like this to be answered in java   9-8 LAB- Parsing food data.pdf
I would like this to be answered in java 9-8 LAB- Parsing food data.pdf
 
Eo gaddis java_chapter_02_5e
Eo gaddis java_chapter_02_5eEo gaddis java_chapter_02_5e
Eo gaddis java_chapter_02_5e
 
Perfomatix - Java Coding Standards
Perfomatix - Java Coding StandardsPerfomatix - Java Coding Standards
Perfomatix - Java Coding Standards
 

More from arkurkuri

Find an artticle or advertisment that exemplifies the use of the sta.pdf
Find an artticle or advertisment that exemplifies the use of the sta.pdfFind an artticle or advertisment that exemplifies the use of the sta.pdf
Find an artticle or advertisment that exemplifies the use of the sta.pdfarkurkuri
 
A researcher is studying the mean weight of a killer whale. She coll.pdf
A researcher is studying the mean weight of a killer whale. She coll.pdfA researcher is studying the mean weight of a killer whale. She coll.pdf
A researcher is studying the mean weight of a killer whale. She coll.pdfarkurkuri
 
Date Due Feb 8. 2017 Students Name Please name and fill in t.pdf
Date Due Feb 8. 2017 Students Name Please name and fill in t.pdfDate Due Feb 8. 2017 Students Name Please name and fill in t.pdf
Date Due Feb 8. 2017 Students Name Please name and fill in t.pdfarkurkuri
 
Compare and contrast passive transport and active transport Compare .pdf
Compare and contrast passive transport and active transport  Compare .pdfCompare and contrast passive transport and active transport  Compare .pdf
Compare and contrast passive transport and active transport Compare .pdfarkurkuri
 
42. What does a coefficient of correlation of 0.70 infer A) Almost .pdf
42. What does a coefficient of correlation of 0.70 infer A) Almost .pdf42. What does a coefficient of correlation of 0.70 infer A) Almost .pdf
42. What does a coefficient of correlation of 0.70 infer A) Almost .pdfarkurkuri
 
4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdf
4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdf4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdf
4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdfarkurkuri
 
Use the given information to find the values of sin 2theta. cos2theta.pdf
Use the given information to find the values of sin 2theta. cos2theta.pdfUse the given information to find the values of sin 2theta. cos2theta.pdf
Use the given information to find the values of sin 2theta. cos2theta.pdfarkurkuri
 
What is the command to transfer the file stored in ~lab3README fro.pdf
What is the command to transfer the file stored in ~lab3README fro.pdfWhat is the command to transfer the file stored in ~lab3README fro.pdf
What is the command to transfer the file stored in ~lab3README fro.pdfarkurkuri
 
2.51. Why does earth have seasons2. What is the origin of the t.pdf
2.51. Why does earth have seasons2. What is the origin of the t.pdf2.51. Why does earth have seasons2. What is the origin of the t.pdf
2.51. Why does earth have seasons2. What is the origin of the t.pdfarkurkuri
 
Write the product as a sum or difference. Write the product as a sum.pdf
Write the product as a sum or difference. Write the product as a sum.pdfWrite the product as a sum or difference. Write the product as a sum.pdf
Write the product as a sum or difference. Write the product as a sum.pdfarkurkuri
 
Which osmotic and ionic strategies must marine teleost fishes employ.pdf
Which osmotic and ionic strategies must marine teleost fishes employ.pdfWhich osmotic and ionic strategies must marine teleost fishes employ.pdf
Which osmotic and ionic strategies must marine teleost fishes employ.pdfarkurkuri
 
What types of social and culture trends affect REISolution1. .pdf
What types of social and culture trends affect REISolution1. .pdfWhat types of social and culture trends affect REISolution1. .pdf
What types of social and culture trends affect REISolution1. .pdfarkurkuri
 
Which of the following are in the order Primates (Multiple answers).pdf
Which of the following are in the order Primates (Multiple answers).pdfWhich of the following are in the order Primates (Multiple answers).pdf
Which of the following are in the order Primates (Multiple answers).pdfarkurkuri
 
What are the issues and benefits of employing older workers Wh.pdf
What are the issues and benefits of employing older workers Wh.pdfWhat are the issues and benefits of employing older workers Wh.pdf
What are the issues and benefits of employing older workers Wh.pdfarkurkuri
 
What is current I1What is current I2What is current I3 14 V 1.pdf
What is current I1What is current I2What is current I3 14 V 1.pdfWhat is current I1What is current I2What is current I3 14 V 1.pdf
What is current I1What is current I2What is current I3 14 V 1.pdfarkurkuri
 
Use your thoughts about the essay, , and your own reflections on y.pdf
Use your thoughts about the essay, , and your own reflections on y.pdfUse your thoughts about the essay, , and your own reflections on y.pdf
Use your thoughts about the essay, , and your own reflections on y.pdfarkurkuri
 
12-1 Group Work Preliminary and Program Development ResearchPurp.pdf
12-1 Group Work Preliminary and Program Development ResearchPurp.pdf12-1 Group Work Preliminary and Program Development ResearchPurp.pdf
12-1 Group Work Preliminary and Program Development ResearchPurp.pdfarkurkuri
 
This homework consists of 8 problems. The first 7ask that .pdf
This homework consists of 8 problems. The first 7ask that .pdfThis homework consists of 8 problems. The first 7ask that .pdf
This homework consists of 8 problems. The first 7ask that .pdfarkurkuri
 
this is verilog HDL classWhat are the basic components of a module.pdf
this is verilog HDL classWhat are the basic components of a module.pdfthis is verilog HDL classWhat are the basic components of a module.pdf
this is verilog HDL classWhat are the basic components of a module.pdfarkurkuri
 
The Hox genes are responsible for determining the anterior-posterior.pdf
The Hox genes are responsible for determining the anterior-posterior.pdfThe Hox genes are responsible for determining the anterior-posterior.pdf
The Hox genes are responsible for determining the anterior-posterior.pdfarkurkuri
 

More from arkurkuri (20)

Find an artticle or advertisment that exemplifies the use of the sta.pdf
Find an artticle or advertisment that exemplifies the use of the sta.pdfFind an artticle or advertisment that exemplifies the use of the sta.pdf
Find an artticle or advertisment that exemplifies the use of the sta.pdf
 
A researcher is studying the mean weight of a killer whale. She coll.pdf
A researcher is studying the mean weight of a killer whale. She coll.pdfA researcher is studying the mean weight of a killer whale. She coll.pdf
A researcher is studying the mean weight of a killer whale. She coll.pdf
 
Date Due Feb 8. 2017 Students Name Please name and fill in t.pdf
Date Due Feb 8. 2017 Students Name Please name and fill in t.pdfDate Due Feb 8. 2017 Students Name Please name and fill in t.pdf
Date Due Feb 8. 2017 Students Name Please name and fill in t.pdf
 
Compare and contrast passive transport and active transport Compare .pdf
Compare and contrast passive transport and active transport  Compare .pdfCompare and contrast passive transport and active transport  Compare .pdf
Compare and contrast passive transport and active transport Compare .pdf
 
42. What does a coefficient of correlation of 0.70 infer A) Almost .pdf
42. What does a coefficient of correlation of 0.70 infer A) Almost .pdf42. What does a coefficient of correlation of 0.70 infer A) Almost .pdf
42. What does a coefficient of correlation of 0.70 infer A) Almost .pdf
 
4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdf
4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdf4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdf
4. On an illegal wolf ranch in Wyoming, gray wolves are bred and sol.pdf
 
Use the given information to find the values of sin 2theta. cos2theta.pdf
Use the given information to find the values of sin 2theta. cos2theta.pdfUse the given information to find the values of sin 2theta. cos2theta.pdf
Use the given information to find the values of sin 2theta. cos2theta.pdf
 
What is the command to transfer the file stored in ~lab3README fro.pdf
What is the command to transfer the file stored in ~lab3README fro.pdfWhat is the command to transfer the file stored in ~lab3README fro.pdf
What is the command to transfer the file stored in ~lab3README fro.pdf
 
2.51. Why does earth have seasons2. What is the origin of the t.pdf
2.51. Why does earth have seasons2. What is the origin of the t.pdf2.51. Why does earth have seasons2. What is the origin of the t.pdf
2.51. Why does earth have seasons2. What is the origin of the t.pdf
 
Write the product as a sum or difference. Write the product as a sum.pdf
Write the product as a sum or difference. Write the product as a sum.pdfWrite the product as a sum or difference. Write the product as a sum.pdf
Write the product as a sum or difference. Write the product as a sum.pdf
 
Which osmotic and ionic strategies must marine teleost fishes employ.pdf
Which osmotic and ionic strategies must marine teleost fishes employ.pdfWhich osmotic and ionic strategies must marine teleost fishes employ.pdf
Which osmotic and ionic strategies must marine teleost fishes employ.pdf
 
What types of social and culture trends affect REISolution1. .pdf
What types of social and culture trends affect REISolution1. .pdfWhat types of social and culture trends affect REISolution1. .pdf
What types of social and culture trends affect REISolution1. .pdf
 
Which of the following are in the order Primates (Multiple answers).pdf
Which of the following are in the order Primates (Multiple answers).pdfWhich of the following are in the order Primates (Multiple answers).pdf
Which of the following are in the order Primates (Multiple answers).pdf
 
What are the issues and benefits of employing older workers Wh.pdf
What are the issues and benefits of employing older workers Wh.pdfWhat are the issues and benefits of employing older workers Wh.pdf
What are the issues and benefits of employing older workers Wh.pdf
 
What is current I1What is current I2What is current I3 14 V 1.pdf
What is current I1What is current I2What is current I3 14 V 1.pdfWhat is current I1What is current I2What is current I3 14 V 1.pdf
What is current I1What is current I2What is current I3 14 V 1.pdf
 
Use your thoughts about the essay, , and your own reflections on y.pdf
Use your thoughts about the essay, , and your own reflections on y.pdfUse your thoughts about the essay, , and your own reflections on y.pdf
Use your thoughts about the essay, , and your own reflections on y.pdf
 
12-1 Group Work Preliminary and Program Development ResearchPurp.pdf
12-1 Group Work Preliminary and Program Development ResearchPurp.pdf12-1 Group Work Preliminary and Program Development ResearchPurp.pdf
12-1 Group Work Preliminary and Program Development ResearchPurp.pdf
 
This homework consists of 8 problems. The first 7ask that .pdf
This homework consists of 8 problems. The first 7ask that .pdfThis homework consists of 8 problems. The first 7ask that .pdf
This homework consists of 8 problems. The first 7ask that .pdf
 
this is verilog HDL classWhat are the basic components of a module.pdf
this is verilog HDL classWhat are the basic components of a module.pdfthis is verilog HDL classWhat are the basic components of a module.pdf
this is verilog HDL classWhat are the basic components of a module.pdf
 
The Hox genes are responsible for determining the anterior-posterior.pdf
The Hox genes are responsible for determining the anterior-posterior.pdfThe Hox genes are responsible for determining the anterior-posterior.pdf
The Hox genes are responsible for determining the anterior-posterior.pdf
 

Recently uploaded

diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppCeline George
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
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
 

Recently uploaded (20)

diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
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
 

JAVACountOccurrenceOfWords.javaYour program should take a text f.pdf

  • 1. JAVA CountOccurrenceOfWords.java Your program should take a text file name as a command line argument. It should read the file and print the number of times the most-used word appears and, in ascending order, the words that occur most frequently. Ignore case when processing the words in the file. Your program may ignore punctuation. For example, if the file contained The program's output should be Words that appear 2 times: and are dachshunds dogs Solution //CountOccurrenceOfWords.java import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.ArrayList; import java.util.*; public class CountOccurrenceOfWords { public static void main(String[] file) throws FileNotFoundException, IOException { if (file.length == 0) { System.out.println("Usage: java CountOccurrenceOfWords targetfile"); System.exit(0); } BufferedReader br = null; br = new BufferedReader(new FileReader(file[0])); String text = null; TreeMap treemap = new TreeMap();
  • 2. try {output while ((text = br.readLine()) != null) { String[] line = text.split("[ t .,;:!?(){}]"); for (int count = 0; count < line.length; count++) { String key = line[count].toLowerCase(); if (key.length() > 0) { if (treemap.get(key) == null) { treemap.put(key, 1); } else { int value = treemap.get(key).intValue(); value++; treemap.put(key, value); } } } } Set> entrySet = treemap.entrySet(); int max = 0; for (Map.Entry entry : entrySet) { if(entry.getValue() > max) max = entry.getValue(); } // create an empty array list with an initial capacity ArrayList maxFrequencyList = new ArrayList(100); for (Map.Entry entry : entrySet) {
  • 3. if(max == entry.getValue()) maxFrequencyList.add(entry.getKey()); } // sort maximum frequncy list Collections.sort(maxFrequencyList); System.out.print("Words that appear " + max + " times are: "); for (String word : maxFrequencyList) { System.out.print(word + ", "); } System.out.println(); } catch (IOException error) { System.out.println("Invalid File"); } finally { br.close(); } } } /* output: Words that appear 2 times are: and, are, dachshunds, dogs, */