d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Do This?
Add Reply New Topic New Poll
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Oct 2 2020 08:19pm
Code
/*Program to return the most common word in a text file using hashmap */



package com.ferfykins.exercises;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CommonWord {

public static void main(String[] args) {

String path = "/home/ferfykins273/Desktop/MyJavaCode/Exercises/SearchText/src/com/ferfykins/exercises/search.txt";

readFile(path);


}



public static void readFile(String path) {
Map<String, Integer> wordMap = new HashMap<>();
String line;
int initialValue = 1;


try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while((line = br.readLine()) != null){
String[] data = line.split(" ");

for(String words : data) {
if(wordMap.containsKey(words)) {
wordMap.replace(words, initialValue+1);
initialValue++;
} else {
wordMap.put(words, initialValue);
}
}



}

System.out.println(searchItem + " was found " + wordCount + " times!!");


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}



}







basically what i wanna do is use a hashmap, and put each word in the text file as a key, and the value would be the amount of times the word occurs.....
So what i wanna do is first check if the key exists, if it does exist, increment the value... and if the key does not exist yet, then add it as a key (each word in the text file)

Hopefully this make sense...... So i understand the logic behind it, but i'm not sure how to do it syntax wise... thanks guys.

This post was edited by ferf on Oct 2 2020 08:41pm
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Oct 2 2020 08:53pm
Still working on it... have a better solution so far, but not done yet


/*Program to return the most common word in a text file using hashmap */

Code
package com.ferfykins.exercises;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CommonWord {

public static void main(String[] args) {

String path = "/home/ferfykins273/Desktop/MyJavaCode/Exercises/SearchText/src/com/ferfykins/exercises/search.txt";

readFile(path);


}



public static void readFile(String path) {
Map<String, Integer> wordMap = new HashMap<>();
String line;



try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while((line = br.readLine()) != null){
String[] data = line.split(" ");

for(String words : data) {
if(wordMap.containsKey(words)) {
int lastValue = wordMap.get(words);
lastValue++;
wordMap.replace(words, lastValue);
} else {
wordMap.put(words, 1);
}
}



}

System.out.println(searchItem + " was found " + wordCount + " times!!");


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}



}






now i gotta figure out how to find which word has the highest value, and print that key out with it's value.

This post was edited by ferf on Oct 2 2020 08:57pm
Member
Posts: 31,293
Joined: Mar 25 2009
Gold: 0.00
Oct 5 2020 09:39pm
figured it out with the help of kippet :)









Code

/*Program to return the most common word in a text file using hashmap.
* Mapping everyword to key, and value to how many times the word occurs.
* Then iterate thru the hashmap to find the highest value */


package com.ferfykins.exercises;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CommonWord {

public static void main(String[] args) {

String path = "/home/ferfykins273/Desktop/MyJavaCode/Exercises/SearchText/src/com/ferfykins/exercises/search.txt";

readFile(path);


}


public static void readFile(String path) {
Map<String, Integer> wordMap = new HashMap<>();
String line;


try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
String[] data = line.split(" ");

for (String words : data) {
if (wordMap.containsKey(words)) {
int lastValue = wordMap.get(words);
lastValue++;
wordMap.replace(words, lastValue);
} else {
wordMap.put(words, 1);
}
}


}


Map.Entry<String, Integer> toBe = null;
for (Map.Entry<String, Integer> entry : wordMap.entrySet()) {
if (toBe == null) {
toBe = entry;
}
if (entry.getValue() > toBe.getValue()) {
toBe = entry;
}
System.out.println(entry.getKey() + ":" + entry.getValue());

}
System.out.println("The highest Key is: " + "\"" + toBe.getKey() + "\"" + " with the value: " + toBe.getValue());


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}


}

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll