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);
}
}
}
Related
I will ask this again. I have this problem which is to create a program that would read a string input from the user (sentence or word). And the Nth number (from the user) will turn into upper case and the rest will be in lowercase.
Example:
string = "good morning everyone"
n = 2
Output = gOod mOrning eVeryone
for (int x = 0; x < s.length(); x++)
if (x == n-1){
temp+=(""+s.charAt(x)).toUpperCase();
}else{
temp+=(""+s.charAt(x)).toLowerCase();
}
s=temp;
System.out.println(s);
}
Output: gOod morning everyone
I know what you want to happen - but you didn't phrase your question very well. The only part your missing is iterating through every word in the sentence. If you asked "how do I apply a function on every word in a String" you likely would have gotten a better response.
This is a bit sloppy since it adds a trailing " " to the end - but you could fix that easily.
public class Test {
static String test = "This is a test.";
public static void main(String[] args) {
String[] words = test.split(" ");
String result = "";
for (String word : words) {
result += nthToUpperCase(word, 2);
result += " ";
}
System.out.println(result);
}
public static String NthToUpperCase(String s, int n) {
String temp = "";
for (int i = 0; i < s.length(); i++) {
if (i == (n-1)) {
temp+=Character.toString(s.charAt(i)).toUpperCase();
} else {
temp+=Character.toString(s.charAt(i));
}
}
return temp;
}
}
You can do this with two for loops. Iterate over each word and within the iteration iterate over each character.
toUpperCase(2, "good morning everyone");
private static void toUpperCase(int nth, String sentence) {
StringBuilder result = new StringBuilder();
for(String word : sentence.split(" ")) {
for(int i = 0; i < word.length(); i++) {
if(i > 0 && i % nth - 1 == 0) {
result.append(Character.toString(word.charAt(i)).toUpperCase());
} else {
result.append(word.charAt(i));
}
}
result.append(" ");
}
System.out.println(result);
}
gOoD mOrNiNg eVeRyOnE
I wrote that code and it's working. But I need to refactor it. I can use only simple methods for solving the problem, for example: "for" loops and simple array.
public class Anagram {
public static void main(String[] args) throws IOException {
Anagram anagrama = new Anagram();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));) {
System.out.println("Enter word or phrase: ");
String userText = reader.readLine();
String resultAnagrama = anagrama.makeAnagram(userText);
System.out.println("Result of Anagrama : " + resultAnagrama);
}
}
This method take user's text and make anagram, but all non-letters should stay on the same places
/**
* #param text
* #return reversed text and all non-letter symbols stay on the same places
*/
public String makeAnagram(String text) {
HashMap<Integer, Character> mapNonLetters;
String[] textFragments = text.split(" ");
StringBuilder stringBuilder = new StringBuilder();
//Check each elements of array for availability symbols and make reverse of elements
for (int i = 0; i < textFragments.length; i++) {
char[] arrayCharacters = textFragments[i].toCharArray();
mapNonLetters = saerchNonLetters(arrayCharacters); // search symbols
StringBuilder builderAnagramString = new StringBuilder(textFragments[i]);
//Delete all non-letters from element of array
int reindexing = 0;
for (HashMap.Entry<Integer, Character> entry : mapNonLetters.entrySet()) {
int key = entry.getKey();
builderAnagramString.deleteCharAt(key - reindexing);
reindexing ++;
}
builderAnagramString.reverse();
//Insert all non-letters in the same places where ones stood
for (HashMap.Entry<Integer, Character> entry : mapNonLetters.entrySet()) {
int key = entry.getKey();
char value = entry.getValue();
builderAnagramString.insert(key, value);
}
textFragments[i] = builderAnagramString.toString();
stringBuilder.append(textFragments[i]);
if (i != (textFragments.length - 1)) {
stringBuilder.append(" ");
}
mapNonLetters.clear();
}
return stringBuilder.toString();
}
This method search all non-letters from each worв of user's text
/**
* Method search symbols
* #param arrayCharacters
* #return HashMap with symbols found from elements of array
*/
public HashMap<Integer, Character> saerchNonLetters(char[] arrayCharacters) {
HashMap<Integer, Character> mapFoundNonLetters = new HashMap<Integer, Character>();
for (int j = 0; j < arrayCharacters.length; j++) {
//Letters lay in scope 65-90 (A-Z) and 97-122 (a-z) therefore other value is non-letter
if (arrayCharacters[j] < 65 || (arrayCharacters[j] > 90 && arrayCharacters[j] < 97) ||
arrayCharacters[j] > 122) {
mapFoundNonLetters.put(j, arrayCharacters[j]);
}
}
return mapFoundNonLetters;
}
}
public class Anagram {
public static void main(String[] args) {
String text = "!Hello123 ";
char[] chars = text.toCharArray();
int left = 0;
int right = text.length() - 1;
while (left < right) {
boolean isLeftLetter = Character.isLetter(chars[left]);
boolean isRightLetter = Character.isLetter(chars[right]);
if (isLeftLetter && isRightLetter) {
swap(chars, left, right);
left++;
right--;
} else {
if (!isLeftLetter) {
left++;
}
if (!isRightLetter) {
right--;
}
}
}
String anagram = new String(chars);
System.out.println(anagram);
}
private static void swap(char[] chars, int index1, int index2) {
char c = chars[index1];
chars[index1] = chars[index2];
chars[index2] = c;
}
}
If I understand correctly and you need only 1 anagram, this should work:
String originalString = "This is 1 sentence with 2 numbers!";
System.out.println("original: "+originalString);
// make a mask to keep track of where the non letters are
char[] mask = originalString.toCharArray();
for(int i=0; i<mask.length; i++)
mask[i] = Character.isLetter(mask[i]) ? '.' : mask[i];
System.out.println("mask: "+ new String(mask));
// remove non letters from the string
StringBuilder sb = new StringBuilder();
for(int i=0; i< originalString.length(); i++) {
if(mask[i] == '.')
sb.append(originalString.charAt(i));
}
// find an anagram
String lettersOnlyAnagram = sb.reverse().toString();
// reinsert the non letters at their place
int letterIndex = 0;
for(int i=0; i<mask.length; i++) {
if(mask[i] == '.') {
mask[i] = lettersOnlyAnagram.charAt(letterIndex);
letterIndex++;
}
}
String anagram = new String(mask);
System.out.println("anagram: "+ anagram);
It prints out:
original: This is 1 sentence with 2 numbers!
mask: .... .. 1 ........ .... 2 .......!
anagram: sreb mu 1 nhtiwecn etne 2 ssisihT!
I have a string
"Red apple, blue banana, orange".
How could I split it by ", " first then add "_" between two words (such as Red_apple but not orange) and capitalize all letters. I read a few posts and found a solution but it only has the split part and how could I also add "_" and capitalize all letters? :
Pattern pattern = Pattern.compile(", ");
List<Fruit> f = pattern.splitAsStream(fruitString)
.map(Fruit::valueOf)
.collect(Collectors.toList());
Fruit is a enum object. So basically if I am able to convert a string to a certain format and I am able get a Enum object based on a Enum name.
Use map(...) method to perform transformations on the original String. Instead of calling Fruit::valueOf through a method reference, split each string on space inside map(...), and construct a combined string when you get exactly two parts:
List<Fruit> f = pattern.splitAsStream("Red apple, blue banana, orange")
.map(s -> {
String[] parts = s.split(" ");
String tmp = parts.length == 2
? parts[0]+"_"+parts[1]
: s;
return Fruit.valueOf(tmp.toUpperCase());
}).collect(Collectors.toList());
Demo.
If you need to perform any additional transformations of the result, you can do them in the same lambda code block prior to the return statement.
Here is another sample:
f = pattern.splitAsStream(fruitString)
.map(s -> Arrays.stream(s.split(" ")).map(String::toUpperCase).collect(Collectors.joining("_")))
.map(Fruit::valueOf).collect(Collectors.toList());
Or by StreamEx:
StreamEx.split(fruitString, ", ")
.map(s -> StreamEx.split(s, " ").map(String::toUpperCase).joining("_"))
.map(Fruit::valueOf).toList();
Your Enum
static enum Fruit {
RED_APPLE, BLUE_BANANA, ORANGE
}
Main code:
public static void main(String[] ar) throws Exception {
Pattern pattern = Pattern.compile(", ");
List<Fruit> f = pattern.splitAsStream("Red apple, blue banana, orange")
.map(YourClass::mapToFruit)
.collect(Collectors.toList());
System.out.println(f);
}
Helper method to offload dirty mapping part
private static Fruit mapToFruit(String input) {
String[] words = input.split("\\s");
StringBuilder sb = new StringBuilder();
if (words.length > 1) {
for (int i = 0; i < words.length - 1; i++) {
sb.append(words[i].toUpperCase());
sb.append("_");
}
sb.append(words[words.length - 1].toUpperCase());
} else {
sb.append(words[0].toUpperCase());
}
return Fruit.valueOf(sb.toString());
}
To split the string you can do:
string[] output = fruitString.split(",");
You would then have to go through the string letter by letter to find spaces and replace them with strings:`
for(int i = 0; i < output.length; i++){
for(int j = 0; j < output[i].length(); j++){
char c = output[i].charAt(j);
//check for space and replace with _
}
}
then using the .toUpperCase() to conver the first char to a upper letter
Hope this helps you.
Please find the below Code, I have followed the below Steps :
1) Split the String by , first.
2) Again split the result of 1) String by " ".
3) Then if the word counts are more than 1 then only proceed to append the underscore.
Demo:
http://rextester.com/NNDF87070
import java.util.*;
import java.lang.*;
class Rextester
{
public static int WordCount(String s){
int wordCount = 0;
boolean word = false;
int endOfLine = s.length() - 1;
for (int i = 0; i < s.length(); i++) {
// if the char is a letter, word = true.
if (Character.isLetter(s.charAt(i)) && i != endOfLine) {
word = true;
// if char isn't a letter and there have been letters before,
// counter goes up.
} else if (!Character.isLetter(s.charAt(i)) && word) {
wordCount++;
word = false;
// last word of String; if it doesn't end with a non letter, it
// wouldn't count without this.
} else if (Character.isLetter(s.charAt(i)) && i == endOfLine) {
wordCount++;
}
}
return wordCount;
}
public static void main(String args[])
{
String cord = "Red apple , blue banana, orange";
String[] parts = cord.split(",");
String[] result1 = new String[parts.length];
for(int i=0; i<parts.length;i++) {
String[] part2 = parts[i].split(" ");
if(parts[i].length() > 1 && WordCount(parts[i]) > 1)
{
String result = "_";
String uscore = "_";
for(int z =0; z < part2.length; z++)
{
if(part2.length > 1 ) {
if (z + 1 < part2.length) {
result = part2[z] + uscore + part2[z + 1];
}
}
}
result1[i] = result.toUpperCase();
}
else
{
result1[i] = parts[i];
}
}
for(int j =0 ; j <parts.length; j++)
{
System.out.println(result1[j]);
}
}
}
References for the WordCount Method: Count words in a string method?
String yourString = "Red apple, blue banana, orange";
stringArray = yourString.split(", ");
List<string> result;
//stringArray will contain 3 strings
//Red apple
//blue banana
//orange
for(string s : stringArray) {
//Replace all spaces with underscores
result.add(s.replace(" ", "_").toUpperCase());
}
I am trying to reverse every 2nd words of every single sentence like
If a given string is :
My name is xyz
The desired output should be :
My eman is zyx
My current output is:
Ym eman s1 zyx
I am not able to achieve my desired output.Don't know what I am doing wrong here
Here is my code
char[] sentence = " Hi my name is person!".toCharArray();
System.out.println(ReverseSentence(sentence));
}
private static char[] ReverseSentence(char[] sentence)
{
//Given: "Hi my name is person!"
//produce: "iH ym eman si !nosrep"
if(sentence == null) return null;
if(sentence.length == 1) return sentence;
int startPosition=0;
int counter = 0;
int sentenceLength = sentence.length-1;
//Solution handles any amount of spaces before, between words etc...
while(counter <= sentenceLength)
{
if(sentence[counter] == ' ' && startPosition != -1 || sentenceLength == counter) //Have passed over a word so upon encountering a space or end of string reverse word
{
//swap from startPos to counter - 1
//set start position to -1 and increment counter
int begin = startPosition;
int end;
if(sentenceLength == counter)
{
end = counter;
}
else
end = counter -1;
char tmp;
//Reverse characters
while(end >= begin){
tmp = sentence[begin];
sentence[begin] = sentence[end];
sentence[end] = tmp;
end--; begin++;
}
startPosition = -1; //flag used to indicate we have no encountered a character of a string
}
else if(sentence[counter] !=' ' && startPosition == -1) //first time you encounter a letter in a word set the start position
{
startPosition = counter;
}
counter++;
}
return sentence;
}
If you want to reverse the alternate word you can try something like splitting the whole String into words delimited by whitespaces and apply StringBuilder reverse() on every second word like :-
String s = "My name is xyz";
String[] wordsArr = s.split(" "); // broke string into array delimited by " " whitespace
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i< wordsArr.length; i++){ // loop over array length
if(i%2 == 0) // if 1st word, 3rd word, 5th word..and so on words
sb.append(wordsArr[i]); // add the word as it is
else sb.append(new StringBuilder(wordsArr[i]).reverse()); // else use StringBuilder revrese() to reverse it
sb.append(" ");// add a whitespace in between words
}
System.out.println(sb.toString().trim()); //remove extra whitespace from the end and convert StringBuilder to String
Output :- My eman is zyx
You can solve your problem vary easy way! Just use a flag variable which will indicate the even or odd position, more precisely whether any word will gonna be reversed or not!
Look at the following modification I made in your code, just added three extra line:
private static boolean flag = true;// added a variable flag to check if we reverse the word or not.
private static char[] ReverseSentence(char[] sentence)
{
//Given: "Hi my name is person!"
//produce: "iH ym eman si !nosrep"
if(sentence == null) return null;
if(sentence.length == 1) return sentence;
int startPosition=0;
int counter = 0;
int sentenceLength = sentence.length-1;
//Solution handles any amount of spaces before, between words etc...
while(counter <= sentenceLength)
{
if(sentence[counter] == ' ' && startPosition != -1 || sentenceLength == counter) //Have passed over a word so upon encountering a space or end of string reverse word
{
flag = !flag; // first time (odd position) we are not going to reverse!
//swap from startPos to counter - 1
//set start position to -1 and increment counter
int begin = startPosition;
int end;
if(sentenceLength == counter)
{
end = counter;
}
else
end = counter -1;
char tmp;
//Reverse characters
while(end >= begin & flag){ //lets see whether we are going to reverse or not
tmp = sentence[begin];
sentence[begin] = sentence[end];
sentence[end] = tmp;
end--; begin++;
}
startPosition = -1; //flag used to indicate we have no encountered a character of a string
}
else if(sentence[counter] !=' ' && startPosition == -1) //first time you encounter a letter in a word set the start position
{
startPosition = counter;
}
counter++;
}
return sentence;
}
Input
My name is xyz
Output:
My eman is zyx
The following code does this "special reverse" which reverses any other word in the sentence:
public static void main(String[] args) {
String sentence = "My name is xyz";
System.out.println(specialReverse(sentence)); // My eman is zyx
}
private static String specialReverse(String sentence) {
String result = "";
String[] words = sentence.split(" ");
// we'll reverse only every second word according to even/odd index
for (int i = 0; i < words.length; i++) {
if (i % 2 == 1) {
result += " " + reverse(words[i]);
} else {
result += " " + words[i];
}
}
return result;
}
// easiest way to reverse a string in Java in a "one-liner"
private static String reverse(String word) {
return new StringBuilder(word).reverse().toString();
}
Just for completeness here's Java-8 solution:
public static String reverseSentence(String input) {
String[] words = input.split(" ");
return IntStream.range(0, words.length)
.mapToObj( i -> i % 2 == 0 ? words[i] :
new StringBuilder(words[i]).reverse().toString())
.collect(Collectors.joining(" "));
}
reverseSentence("My name is xyz"); // -> My eman is zyx
package com.eg.str;
// Without using StringBuilder
// INPUT: "Java is very cool prog lang"
// OUTPUT: Java si very looc prog gnal
public class StrRev {
public void reverse(String str) {
String[] tokens = str.split(" ");
String result = "";
String k = "";
for(int i=0; i<tokens.length; i++) {
if(i%2 == 0)
System.out.print(" " + tokens[i] + " ");
else
result = tokens[i];
for (int j = result.length()-1; j >= 0; j--) {
k = "" + result.charAt(j);
System.out.print(k);
}
result = "";
}
}
public static void main(String[] args) {
StrRev obj = new StrRev();
obj.reverse("Java is very cool prog lang");
}
}
//reverse second word of sentence in java
public class ReverseSecondWord {
public static void main(String[] args) {
String s="hello how are you?";
String str[]=s.split(" ");
String rev="";
for(int i=0;i<str[1].length();i++)
{
char ch=str[1].charAt(i);
rev=ch+rev;
}
str[1]=rev;
for(int i=0;i<str.length;i++)
{
System.out.print(str[i]+" ");
}
}
}
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();
}
}
}
}