I need to find second to last word in each line (they are divided by space) and find 3 most popular of them and find how many are there? Can you help me in any way?
Input example:
abcd i
asd ffdds abcd ddd ?
abcd ffdds asd ddd i
ddd abcd i
a f g w e a asdfasdasdas fdd i
Answer that I need:
abcd 2
ddd 2
fdd 1
or
2 abcd
2 ddd
1 fdd
This is my code
public class asdf {
public static void main(String args[]) throws IOException {
BufferedReader in = new BufferedReader(new FileReader("input.txt"));
String str;
List < String > list = new ArrayList < String > ();
while ((str = in .readLine()) != null) {
if (str.startsWith(" ") && str.endsWith("i") || str.endsWith("?")) {
list.add(str);
}
}
String[] stringArr = list.toArray(new String[0]); //for backup
String[] stringArrAC = list.toArray(new String[0]);
for (int i = 0; i < stringArrAC.length; i++) {
stringArrAC[i] = stringArrAC[i].substring(63);
}
//String[] stringArrLAST = (new String[0]);
Map<String, Integer> occurrences = new HashMap();
for (String line : stringArrAC) {
String[] words = line.split(" ");
String nextToLastWord = words[words.length - 2];
occurrences.put(nextToLastWord,
occurrences.get(nextToLastWord) == null
? 1
: occurrences.get(nextToLastWord) + 1);
}
occurrences.entrySet().stream()
// Sort the values in descending order
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
// Gets top 3 entries
.limit(3)
// Print them
.forEach(System.out::println);
try {
PrintWriter pr = new PrintWriter("output.txt");
for (int i = 0; i < stringArrAC.length; i++) {
pr.println(stringArrAC[i]);
}
pr.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println("No such file exists.");
}
}
Java 8 makes this simpler.
Count the occurrences of the next to last word with a HashMap, then use streams to sort the HashMap in descending order and grab the top three values.
public static void main(String[] args) throws Exception {
List<String> lines = new ArrayList() {
{
add("abcd i");
add("asd ffdds abcd ddd ?");
add("abcd ffdds asd ddd i");
add("ddd abcd i");
add("a f g w e a asdfasdasdas fdd i");
add("123 awef bad");
add("123 awef bad");
add("123 awef bad");
add("oneword");
}
};
Map<String, Integer> occurrences = new HashMap();
for (String line : lines) {
// Skip blank lines
if (line.isEmpty()) {
continue;
}
String[] words = line.split(" ");
// Account for a line that might have only one word
String nextToLastWord = words.length >= 2 ? words[words.length - 2] : words[0];
occurrences.put(nextToLastWord,
occurrences.get(nextToLastWord) == null
? 1
: occurrences.get(nextToLastWord) + 1);
}
occurrences.entrySet().stream()
// Sort the values in descending order
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
// Gets top 3 entries
.limit(3)
// Print them
.forEach(System.out::println);
}
Results:
awef=3
ddd=2
abcd=2
Parse each line with a string tokenizer to get an array of words. For each line, the word you want will be the second to last element in the array. Create a Map to store the word in, along with an associated counter that you'll increment by one each time you encounter the same word. In other words, once the word is in the map, if you find that word again, increment its counter in the map. When you're done, get the key value pairs from the map, find the 3 highest counter values, and their associated words.
Related
So i've been trying to take a txt file which has input like this for eg -
abcddhdj
efghdd
ijkl
to get this -
j
d
hd
dd
dhl
cgk
bfj
aei
i have tried to do this using 2d char array which gave nullexception and arrayoutofbound error and didnt work mostly,then tried string array , arraylist of arraylist of char , and lastly i have been trying using arraylsit of string
here is the closest i got to my solution after lot of searching by using string[] -
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt")); // PUT YOUR FILE LOCATION HERE
int k=0,i,j=0,x;
String line[] = new String[10] ; //SET THE APPROXIMATE NUMBER OF ROWS
while((line[k] = br.readLine()) !=null)
{System.out.println(line[k]); //print to check input - verified
k++;
}
for(x=0;x<k;x++)
{if(j<line[x].length())
{j=line[x].length()-1;} //this part not working in above loop
}
System.out.println(j); // verified but not working inside previous loop for some reason
System.out.println(k);
for(x=j-1;x>=0;x++) //without this loop,its perfect, but with it gives indexoutofbound error , doesnt run at x=j
{ for(i=0;i<k;i++)
{ System.out.print(line[i].charAt(x));
}
System.out.println();
}
}
here is one output
run:
abcd
efgh
ijkl
4 //should have come as 3 since i did length-1
3
chl //notice the d missing , every char of first row shifted,just why
bgk //in outofbound error , it only prints d at the end, need explanation
afj
ei
BUILD SUCCESSFUL (total time: 0 seconds)
if i add a space after abcd it gives indexoutofbound and no output after k
at end i used another method which adds spaces to make all length equal
yet still the output was wrong, plus there is something wrong with this way of thinking , there should be better method
so i tried arraylist , this is giving me more problems again
trying to work this out by any method understandable.
This ought to do the trick:
The key here is that I pad all the line arrays with empty chars so that each character array is the same length as the longest line.
public static void main(String[] args)
{
try (BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt")))
{
String line;
List<List<Character>> lines = new ArrayList<>();
int longestLine = 0;
while((line = br.readLine()) !=null)
{
line = line.trim();
if (line.length() > 0)
{
List<Character> currList = new ArrayList<>();
for (char c : line.toCharArray())
{
currList.add(c);
}
if (currList.size() > longestLine)
{
longestLine = currList.size();
}
lines.add(currList);
}
}
// pad all lists to be the same as the longest
for (List<Character> currList : lines)
{
while (currList.size() < longestLine)
{
currList.add(Character.MIN_VALUE);
}
}
// go through each list backwards
for (int i = longestLine - 1; i >= 0; i-- )
{
for (List<Character> currList : lines)
{
System.out.print(currList.get(i));
}
System.out.println();
}
}
catch (Throwable t)
{
t.printStackTrace();
}
}
Example Input:
abcd
efgh
ijkl
g
Example Output:
dhl
cgk
bfj
aeig
Assuming input is read into the arraylist
ArrayList<String> inputList = new ArrayList<String>();
inputList.add("abcddhdj");
inputList.add("efghdd");
inputList.add("ijkl");
int maxSize = 0;
for (String input : inputList) {
if (input.length() > maxSize) {
maxSize = input.length();
}
}
String outputList[] = new String[maxSize];
for (int i = 0; i < maxSize; i++) {
String output = "";
for (String input : inputList) {
if(i<input.length())
output=output+input.charAt(i);
}
outputList[maxSize-(i+1)]=output;
}
Store all to direct 2d array and transpose in printing loop
final char[][] matrix = Files.lines(Paths.get(fileName)).map(String::toCharArray).toArray(char[][]::new);
final int width = Arrays.stream(matrix).mapToInt(a -> a.length).max().getAsInt();
for (int i = 0; i < width; ++i ) {
final int idx = width-i-1;
String s = Arrays.stream(matrix).map(a -> a.length > idx ? String.valueOf(a[idx]) : " ").collect(Collectors.joining());
System.out.println(s);
}
I am fairly new to java. I want to count the occurrences of words in a particular line. So far i can only count the words but no idea how to count occurrences.
Is there a simple way to do this?
Scanner file = new Scanner(new FileInputStream("/../output.txt"));
int count = 0;
while (file.hasNextLine()) {
String s = file.nextLine();
count++;
if(s.contains("#AVFC")){
System.out.printf("There are %d words on this line ", s.split("\\s").length-1);
System.out.println(count);
}
}
file.close();
Output:
There are 4 words on this line 1
There are 8 words on this line 13
There are 3 words on this line 16
Simplest way I can think of is to use String.split("\\s"), which will split based on spaces.
Then have a HashMap containing a word as the key with the value being the number of times it is used.
HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>();
while (file.hasNextLine()) {
String s = file.nextLine();
String[] words = s.split("\\s");
int count;
for (String word : words) {
if (mapOfWords.get(word) == null) {
mapOfWords.put(word, 1);
}
else {
count = mapOfWord.get(word);
mapOfWords.put(word, count + 1);
}
}
}
Implementation you requested to skip strings that contain certain words
HashMap<String, Integer> mapOfWords = new HashMap<String, Integer>();
while (file.hasNextLine()) {
String s = file.nextLine();
String[] words = s.split("\\s");
int count;
if (isStringWanted(s) == false) {
continue;
}
for (String word : words) {
if (mapOfWords.get(word) == null) {
mapOfWords.put(word, 1);
}
else {
count = mapOfWord.get(word);
mapOfWords.put(word, count + 1);
}
}
}
private boolean isStringWanted(String s) {
String[] checkStrings = new String[] {"chelsea", "Liverpool", "#LFC"};
for (String check : checkString) {
if (s.contains(check)) {
return false;
}
}
return true;
}
Try below code, it may solve your problem, in addition you can call String.toLowerCase() before you put it into the hashmap
String line ="a a b b b b a q c c";
...
Map<String,Integer> map = new HashMap<String,Integer>();
Scanner scanner = new Scanner(line);
while (scanner.hasNext()) {
String s = scanner.next();
Integer count = map.put(s,1);
if(count!=null) map.put(s,count + 1);
}
...
System.out.println(map);
Result:
{b=4, c=2, q=1, a=3}
Fastest would be store the splitted data in a ArrayList then iterate on your ArrayList and use [Collections.frequency] (http://www.tutorialspoint.com/java/util/collections_frequency.htm)
Check Guava's Multiset. Their description starts with 'The traditional Java idiom for e.g. counting how many times a word occurs in a document is something like:'. You find some code snippets how to do that without a MultiSet.
BTW: If you only wanted to count the number of words in your string, why not just count the spaces? You could use StringUtils from the apache commons. It's much better than creating an array of the split parts. Also have a look at their implementation.
int count = StringUtils.countMatches(string, " ");
In a given String, occurrences of a given String can be counted using String#indexOf(String, int) and through a loop
String haystack = "This is a string";
String needle = "i";
int index = 0;
while (index != -1) {
index = haystack.indexOf(needle, index + 1);
if (index != -1) {
System.out.println(String.format("Found %s in %s at index %s.", needle, haystack, index));
}
}
I have a method which takes in parameters in the form of a vector from another vector. This vector can be of the size 2, 3 or 4 elements.
I want to count the frequency of every word in that vector. For example, if the vector contained the strings : "hello", "my" , "hello" , I want to output an array that is
[2, 1] where 2 is the frequency of hello and 1 is the frequency of my.
Here is my attempt after reading a few questions on this website:
int vector_length = query.size();
int [] tf_q = new int [vector_length];
int string_seen = 0;
for (int p = 0; p< query.size(); p++)
{
String temp_var = query.get(p);
for (int q = 0; q< query.size(); q++)
{
if (temp_var == query.get(q) )
{
if (string_seen == 0)
{
tf_q[p]++;
string_seen++;
}
else if (string_seen == 1)
{
tf_q[p]++;
string_seen = 0;
query.remove(p);
}
}
}
}
System.out.print(Arrays.toString(tf_q));
What is the right direction to go?
Use a HashMap of type to track the unique string values you encounter that count each word
String[] vector // your vector
Map<String, Integer> stringMap = new HashMap<String, Integer>();
for (int i = 0; i < vector.length; i++) {
if (stringMap.containsKey(vector[i]) {
Integer wordCount = stringMap.get(vector[i]);
stringMap.put(vector[i], new Integer(wordCount + 1));
}
else {
stringMap.put(vector[i], new Integer(1));
}
}
String[] input = {"Hello", "my", "Hello", "apple", "Hello"};
// use hashmap to track the number of strings
HashMap<String, Integer> map = new HashMap<String, Integer>();
// use arraylist to track the sequence of the output
ArrayList<String> list = new ArrayList<String>();
for (String str : input){
if(map.containsKey(str)){
map.put(str, map.get(str)+1);
} else{
map.put(str, 1);
list.add(str); // if the string never occurred before, add it to arraylist
}
}
int[] output = new int[map.size()];
int index = 0;
for (String str : list){
output[index] = map.get(str);
index++;
}
for (int i : output){
System.out.println(i);
}
This should be your answer! Result is in "int[] output"
If you want to maintain the relation between each word and the frequency of that word, then I suggest that you use a HashMap instead. For example:
Map<String,Integer> histogram = new HashMap<String,Integer>();
for (String word : query)
{
Integer count = histogram.get(word);
if (count == null)
histogram.put(word,1);
else
histogram.put(word,count+1);
}
At this point, you can (for example) print each word with the corresponding frequency:
for (String word : histogram.keySet())
System.out.println(word+" "+histogram.get(word));
Or you can obtain an array which contains only the frequencies, if that's all you want:
Integer[] array = histogram.values().toArray(new Integer[histogram.size()]);
Or even a collection, which is just as useful and convenient as any native array:
Collection<Integer> collection = histogram.values();
I've written a program to read from a text file where each line is a word. The first portion of code finds the longest word beginning with each letter of the alphabet and stores it in an array. The 2nd part I want the program to do is find, for each letter of the alphabet, the word with the highest occurrence of that letter.
So the output SHOULD look like this:
Longest words:
a: anthropomorphologically
b: blepharosphincterectomy
c: cholecystenterorrhaphy
d: dacryocystoblennorrhea
e: epididymodeferentectomy
f: formaldehydesulphoxylate
g: gastroenteroanastomosis
h: hematospectrophotometer
i: indistinguishableness
j: jurisprudentialist
k: keratoconjunctivitis
l: laparocolpohysterotomy
m: macracanthrorhynchiasis
n: naphthylaminesulphonic
o: omnirepresentativeness
p: pathologicopsychological
q: quadratomandibular
r: reticulatocoalescent
s: scientificophilosophical
t: tetraiodophenolphthalein
u: ureterocystanastomosis
v: vagoglossopharyngeal
w: weatherproofness
x: xanthocreatinine
y: yohimbinization
z: zoologicoarchaeologist
Words with most letter:
a: astragalocalcaneal
b: beblubber
c: chlorococcaceae
d: disdodecahedroid
e: electrotelethermometer
f: giffgaff
g: cuggermugger
h: choledochorrhaphy
i: impossibilification
j: ajaja
k: akiskemikinik
l: allochlorophyll
m: dynamometamorphism
n: nonannouncement
o: choledochoduodenostomy
p: aplopappus
q: equivoque
r: archcorrupter
s: possessionlessness
t: anticonstitutionalist
u: untumultuous
v: overconservative
w: bowwow
x: adnexopexy
y: dacryocystosyringotomy
z: zizz
}
Basically, I need to figure out how to do it so the output isn't the word with the most letters same as the first letter (like how 'f' [giffgaff] above does not begin with 'f'). I've googled/bing'd a lot and not found anything to help.
/**
* #param args first String argument is the
* name of the input text file
*/
public static void main(String [] args) throws IOException {
//instance variable
String[] longestWords = new String[26];
String[] mostCharsWord = new String[26];
String currentLine = null;
int[] numCharacters = new int[26];
//because the while loop in try statement is comparing lengths in order to
//assign words, I must give each element a non-null value
//in this case, length = 0
Arrays.fill(longestWords, "");
Arrays.fill(mostCharsWord, "");
//try block
try(BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
String currentLongestWord;
int index;
int indexer = 0;
int count = 0;
int counter = 0;
while((currentLine=br.readLine()) != null) {
currentLine = currentLine.toLowerCase();
index = currentLine.charAt(0)-'a';
currentLongestWord = longestWords[index];
if(currentLine.length() > currentLongestWord.length()) {
longestWords[index] = currentLine;
}
/**
* this code below is for the "AND" bit, but I know that it's not correct.
* Instead of printing out the word with the most occurrences of each
* letter, it prints out the word with the most occurrences of each letter
* THAT BEGINS WITH THAT LETTER
*/
for(char c : currentLine.toCharArray()) {
if(c == currentLine.charAt(0)) {
count += 1;
}
}
for(String currentMostCharsWord : mostCharsWord) {
indexer += 1;
for(char c : currentLine.toCharArray()) {
for( char d: currentMostCharsWord.toCharArray()) {
if(c==d) {
//hmmm....this would compare every letter, not just the one
//that I'm looking for. booooooo
}
}
}
}
if(count > numCharacters[index]) {
numCharacters[index] = count;
mostCharsWord[index] = currentLine;
}
count = 0;
}
//close file!
br.close();
}
//catch block
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//finally / do anyway statement
finally {
System.out.println("Longest Words: \n");
for(int j = 0; j < 26; j++) {
System.out.printf("%s: %s\n", longestWords[j].charAt(0), longestWords[j]);
}
System.out.println("------------------------------------\n\nWords with most letters: \n");
for(int j = 0; j < 26; j++) {
System.out.printf("%s: %s\n", mostCharsWord[j].charAt(0), mostCharsWord[j]);
}
}
}
}
You may use something like this:
// Map with the longest word for each letter
Map<Character, String> longestWordMap = new HashMap<Character, String>();
// Map with the word with highest occurrences of each letter
Map<Character, String> mostCharsWordMap = new HashMap<Character, String>();
while((word = br.readLine()) != null) { {
word = word.toLowerCase();
Character beginning = word.charAt(0);
String longestWord = longestWordMap.get(beginning);
// If the current word is the longest, put the word in the map
if (longestWord == null || word.length() > longestWord.length()) {
longestWordMap.put(beginning, word);
}
for (char letter = 'a'; letter <= 'z'; letter++) {
String mostCharsWord = mostCharsWordMap.get(Character.valueOf(letter));
if (mostCharsWord == null ||
characterCount(letter, word) > characterCount(letter, mostCharsWord)) {
mostCharsWordMap.put(Character.valueOf(letter), word);
}
}
}
And here is the function used to count the occurrences of a letter in a word:
public static int characterCount(char letter, String word) {
int characterCount = 0;
for (char c : word.toCharArray()) {
if (c == letter) {
characterCount++;
}
}
return characterCount;
}
There is probably a more straight-forward approach to this. So the problem is in essence that you have a stream of words and based on the word that is currently being read from the stream you compare it against the longest known word you have in your data store. If it is longer you replace the word, else do nothing. Your logic may be to replace it based on something else, such as dictionary ordering of the word. Checking for case sensitivity is left as an exercise to you.
//mostly pseudo-code
public class LongestWord
{
Map<Character,String> longestWords = new HashMap<Character,String>();
while(wordsStream.hasNext())
{
String currentWord = wordStream.next();
String longestWordByLetter = longestWords.get(currentWord.charAt(0));
if(null != longestWordByLetter)
{
if(longestWordByLetter.size() < currentWord.size())
{
longestWords.put(currentWord.charAt(0),currentWord);
}//else do nothing
}else{
longestWords.put(currentWord.charAt(0),currentWord);
}
}
}
I'm having trouble of figuring out how to find the most frequent word and the most frequent case-insensitive word for a program. I have a scanner that reads through the text file and a while loop, but still doesn't know how to implement what I'm trying to find. Do I use a different string function to read and print the word out?
Here is my code as of now:
public class letters {
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("input.txt");
Scanner scanner = new Scanner(fis);
String word[] = new String[500];
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
}
}
String []roll = s.split("\\s");
for(int i=0;i<roll.length;i++){
String lin = roll[i];
//System.out.println(lin);
}
}
This is what I have so far. I need the output to say:
Word:
6 roll
Case-insensitive word:
18 roll
And here is my input file:
#
roll tide roll!
Roll Tide Roll!
ROLL TIDE ROLL!
ROll tIDE ROll!
roll tide roll!
Roll Tide Roll!
ROLL TIDE ROLL!
roll tide roll!
Roll Tide Roll !
#
65-43+21= 43
65.0-43.0+21.0= 43.0
65 -43 +21 = 43
65.0 -43.0 +21.0 = 43.0
65 - 43 + 21 = 43
65.00 - 43.0 + 21.000 = +0043.0000
65 - 43 + 21 = 43
I just need it to find the most occuring word(Which is the maximal consecutive sequence of letters)(which is roll) and print out how many times it is located(which is 6) . If anybody can help me on this, that would be really great! thanks
Consider using a Map<String,Integer> for the word then you can implement this to count words and will be work for any number of words. See Documentation for Map.
Like this (would require modification for case insensitive)
public Map<String,Integer> words_count = new HashMap<String,Integer>();
//read your line (you will have to determine if this line should be split or is equations
//also just noticed that the trailing '!' would need to be removed
String[] words = line.split("\\s+");
for(int i=0;i<words.length;i++)
{
String s = words[i];
if(words_count.ketSet().contains(s))
{
Integer count = words_count.get(s) + 1;
words_count.put(s, count)
}
else
words_count.put(s, 1)
}
Then you have the number of occurrences for each word in the string and to get the most occurring do something like
Integer frequency = null;
String mostFrequent = null;
for(String s : words_count.ketSet())
{
Integer i = words_count.get(s);
if(frequency == null)
frequency = i;
if(i > frequency)
{
frequency = i;
mostFrequent = s;
}
}
Then to print
System.out.println("The word "+ mostFrequent +" occurred "+ frequency +" times");
Start with accumulating all the words into a Map as follows:
...
String[] roll = s.split("\\s+");
for (final String word : roll) {
Integer qty = words.get(word);
if (qty == null) {
qty = 1;
} else {
qty = qty + 1;
}
words.put(word, qty);
}
...
Then you need to figure out which has the biggest score:
String bestWord;
int maxQty = 0;
for(final String word : words.keySet()) {
if(words.get(word) > maxQty) {
maxQty = words.get(word);
bestWord = word;
}
}
System.out.println("Word:");
System.out.println(Integer.toString(maxQty) + " " + bestWord);
And last you need to merge all forms of the same word together:
Map<String, Integer> wordsNoCase = new HashMap<String, Integer>();
for(final String word : words.keySet()) {
Integer qty = wordsNoCase.get(word.toLowerCase());
if(qty == null) {
qty = words.get(word);
} else {
qty += words.get(word);
}
wordsNoCase.put(word.toLowerCase(), qty);
}
words = wordsNoCase;
Then re-run the previous code snippet to find the word with the biggest score.
Try to use HashMap for better results. You need to use BufferedReader and Filereader for taking input file as follows:
FileReader text = new FileReader("file.txt");
BufferedReader textFile = new BufferedReader(text);
The Bufferedreader object textfile needs to passed as a parameter to the method below:
public HashMap<String, Integer> countWordFrequency(BufferedReader textFile) throws IOException
{
/*This method finds the frequency of words in a text file
* and saves the word and its corresponding frequency in
* a HashMap.
*/
HashMap<String, Integer> mapper = new HashMap<String, Integer>();
StringBuffer multiLine = new StringBuffer("");
String line = null;
if(textFile.ready())
{
while((line = textFile.readLine()) != null)
{
multiLine.append(line);
String[] words = line.replaceAll("[^a-zA-Z]", " ").toLowerCase().split(" ");
for(String word : words)
{
if(!word.isEmpty())
{
Integer freq = mapper.get(word);
if(freq == null)
{
mapper.put(word, 1);
}
else
{
mapper.put(word, freq+1);
}
}
}
}
textFile.close();
}
return mapper;
}
The line line.replaceAll("[^a-zA-Z]", " ").toLowerCase().split(" "); is used for replacing all the characters other than alphabets, the it makes all the words in lower case (which solves your case insensitive problem) and then splits the words seperated by spaces.
/*This method finds the highest value in HashMap
* and returns the same.
*/
public int maxFrequency(HashMap<String, Integer> mapper)
{
int maxValue = Integer.MIN_VALUE;
for(int value : mapper.values())
{
if(value > maxValue)
{
maxValue = value;
}
}
return maxValue;
}
The above code returns that value in hashmap which is highest.
/*This method prints the HashMap Key with a particular Value.
*/
public void printWithValue(HashMap<String, Integer> mapper, Integer value)
{
for (Entry<String, Integer> entry : mapper.entrySet())
{
if (entry.getValue().equals(value))
{
System.out.println("Word : " + entry.getKey() + " \nFrequency : " + entry.getValue());
}
}
}
Now you can print the most frequent word along with its frequency as above.
/* i have declared LinkedHashMap containing String as a key and occurrences as a value.
* Creating BufferedReader object
* Reading the first line into currentLine
* Declere while-loop & splitting the currentLine into words
* iterated using for loop. Inside for loop, i have an if else statement
* If word is present in Map increment it's count by 1 else set to 1 as value
* Reading next line into currentLine
*/
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("F:\\chidanand\\javaIO\\Student.txt"));
String currentLine = reader.readLine();
while (currentLine!= null) {
String[] input = currentLine.replaceAll("[^a-zA-Z]", " ").toLowerCase().split(" ");
for (int i = 0; i < input.length; i++) {
if (map.containsKey(input[i])) {
int count = map.get(input[i]);
map.put(input[i], count + 1);
} else {
map.put(input[i], 1);
}
}
currentLine = reader.readLine();
}
String mostRepeatedWord = null;
int count = 0;
for (Entry<String, Integer> m:map.entrySet())
{
if(m.getValue() > count)
{
mostRepeatedWord = m.getKey();
count = m.getValue();
}
}
System.out.println("The most repeated word in input file is : "+mostRepeatedWord);
System.out.println("Number Of Occurrences : "+count);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}