Home Answers Viewqa Java-Beginners Java count occurrence of a word

 
 


ESHWIN GEORGE
Java count occurrence of a word
1 Answer(s)      10 months ago
Posted in : Java Beginners

there is file called "story.txt",in a program we want to count occurrence of a word (example bangalore) in this file and print how many time word is present in the file.

View Answers

July 13, 2012 at 2:56 PM


The given code reads the text file and count the occurrence of each word.

import java.io.*;
import java.util.*;

public class CountWordOccurrence {
        public static void main(String[] args){
try{
       BufferedReader br=new BufferedReader(new FileReader("c:/data.txt"));
       String str="";
        String st;
        while((st=br.readLine())!=null){
            str+=st+" ";
        }
        HashMap<String, Integer> map = new HashMap<String, Integer>();


            str = str.toLowerCase(); 
                int count = -1;
                for (int i = 0; i < str.length(); i++) { 
                   if ((!Character.isLetter(str.charAt(i))) || (i + 1 == str.length())) { 
                            if (i - count > 1) { 
                            if (Character.isLetter(str.charAt(i))) 
                                i++;
                            String word = str.substring(count + 1, i);
                            if (map.containsKey(word)) { 
                            map.put(word, map.get(word) + 1);
                            }
                            else { 
                            map.put(word, 1);
                            } 
                        } 
                        count = i;
                    } 
                } 
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.addAll(map.values());
        Collections.sort(list, Collections.reverseOrder());
        int last = -1;
        for (Integer i : list) { 
            if (last == i) 
                continue;
            last = i;
            for (String s : map.keySet()) { 
                if (map.get(s) == i) 
                    System.out.println(s + ":" + i);
            } 
        }
}
catch(Exception e){
System.out.println(e);
}
    } 
}









Related Pages:
Java Count word occurrence and export it to excel file
Java Count word occurrence and export it to excel file Here is an example of scanning a text file in a local drive, and count the frequency of each word in the text file or you can say count the number or occurrence of each word
Count instances of each word
Count instances of each word  I am working on a Java Project that reads a text file from the command line and outputs an alphabetical listing of the words preceded by the occurrence count. My program compiles and runs
Java Word Occurrence Example
Java Word Occurrence Example In this example we will discuss about the how... can count the occurrences of each word in a file. In this example we will use... will demonstrate you about how to count occurrences of each word in a file. In this example
Java count frequency of words in the string
Java count frequency of words in the string. In this tutorial, you will learn how to count the occurrence of each word in the given string. String...++; String word = str.substring(count + 1, i); if (map.containsKey(word)) { map.put
Java xml count occurrence of elements
Java xml count occurrence of elements Java provides xml parsers to read... parsers for different purposes. Here we will count the number of occurrences... Integer(1)); } else { int count = ((Integer) value).intValue
Java count occurrence of number from array
Java count occurrence of number from array Here we have created an example that will count the occurrence of numbers and find the number which has the highest occurrence. Now for this, we have created an array of 15 integers
Word Count
Word Count       This example counts the number of occurrences of  a specific word...\kodejava>java WordCountExample 3 occurrences of the word 'you' in 'How r you?R you
java word counting - Java Beginners
java word counting  Hi I want a code in java that replaces a word...... but this will change all the occurrence even if its part of another word the run... = new java.util.Scanner(System.in);String s,s1,s2;String resultString;int count
Java Word Count - Word Count Example in Java
Java Word Count - Word Count Example in Java  ...;}   } } Download Word count Example... to count the number of lines, number of words and number of characters
Count Characters from the input string Java
Count Characters from the input string of Java In this section, you will learn how to count the occurrence of each character in the string. For this, we have... to count the occurrence of each character of the array.  Here is the code
Count characters from text file in Java
Count characters from text file Using Java Here we are going to count the occurrence of each character from the text file. For this, we have used... it into character array in order to count the occurrence of each character
word program - Java Beginners
word program  HELLO MAM AND SIR I ASK YOUR HELP HOPE YOU GONNA HELP ME THANK YOU!!Design a program to search a word for letters that the user....(REmember that the alphabet contains 52 characters if you count uppercase and lowercase
Retrieve a list of words from a website and show a word count plus a specified number of most frequently occurring words
Retrieve a list of words from a website and show a word count plus a specified... occurrence of key, word, in the wordCount map...; Integer count; // number of occurrences WordPair(String word
display co-occurrence words in a file
display co-occurrence words in a file  how to write java program for counting co occurred words in the file
display co-occurrence words in a file
display co-occurrence words in a file  how to write java program for counting co occurred words in the file
program to display frequency count of each word in a file using Hashmap, Hashset and streamtokenizer.plz help me out ..
program to display frequency count of each word in a file using Hashmap...()) { // it is a HashSet word String str=iterator.next(); int count=0...++; } System.out.println(str+" word is repeated "+count+" time in given file
Count repetitions of every word from an input file
Count repetitions of every word from an input file  Hello..i got to know how can i count the repetitions of every word present in a specific input... recorded i need to count only the url patterns like google,yahoo etc, plz help me
word and character counting - Java Beginners
word and character counting  here is the java code i made but i have... javax.swing.*; import java.io.*; public class count { public static void main... = null; String word; int numLines = 0; int numWords = 0
count the repeated character in one string
count the repeated character in one string  to find the how character occurrence in one String (for example the string is -java means there is 2...;counter<third.length;counter++){ char ch= third[counter]; int count=0
Example - Replace word
Java: Example - Replace word Problem: Write a method to replaces all occurences a word in a string with another word. Assume the method signature... a new String. The word "replace" is probably misleading. Also, the method
export to word document - Java Beginners
export to word document  hi sir,when i am click on a button under the jtable,for example (print button),then i want to print that jtable in word... actionPerformed(ActionEvent ae){ int count=table.getRowCount
Java String Occurrence in a String
Java String Occurrence in a String       In this program you will learn how to find the occurrence...:\unique>java StringCount 2 C:\unique> Download
Count characters from text file in Java
is a java code that count the occurrence of each character from text file. import...Count characters from text file in Java  At the "Count chracters fro mtext file in Java". I tried to run the code, but the error at the line have
USE ARRAY TO FIND THE LARGEST NUMBER AND OCCURRENCE
and count the occurrence of the largest number number entered from the keyboard... will display the largest number is 6 and the occurrence count is 5. * Also display
Count number of occurences and print names alphabetically in Java
++; String word = str.substring(count + 1, i...Count number of occurences and print names alphabetically in Java  I... for the printCount() method for the code to count the number of occurences of each
count occourance no of time no between 0 to 9 given by user in java
example that accepts 9 integers from the user and count the occurrence of each...count occourance no of time no between 0 to 9 given by user in java  import java.io.*; class count_no { public static void main(String args
Java count characters from file
Java count characters from file In this section, you will learn how to count... into character array in order to count the occurrence of each character... read the file and count the occurrence of each character of the content
Exercise - Count Words
Java: Exercise - Count Words Problem Write a method which counts the number of words in a string. Assume that a word is defined as a sequence of letters... the word count and switch the state of the boolean variable. Extensions
count in java - Java Beginners
, count of frmale is 20. what's code in java to select count from database where...count in java  Hello javamania.... i want to ask something like... is 20. i have JFrame Form(jTextField,jLabel,jButton), i want to get count
Count the character in java
Count the character in java  Write a java program to count....   Count characters by implementing thread import java.util.*; class CountCharacters { public static void count(final String str){ Runnable
Count Rows - JSP-Servlet
Count Rows  How to count rows in Java. Thanks
Compare two word file
Compare two word file  How to compare two word file using Java
Word replacment - Java Beginners
Word replacment  Java code for the Word Replacement.Thnaks in Advance!  Hipublic class WordReplaced{ public static void main(String[] args... information,http://www.roseindia.net/java
Java Word Processor
Java Word Processor  Problem: Design and implement a class called... of words (a hyphenated word is one word), o number of sentences (ends...: ? You can break up a String into words or "tokens" by using java?s Scanner
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
String file in to word file in java
String file in to word file in java  How to convert a String format notepad file into Ms word file in java
String file in to word file in java
String file in to word file in java  how to convert a String format word file into Ms Word file using java? please can anyone of you help me
java
Java count occurrence of a word  there is file called "story.txt",in a program we want to count occurrence of a word (example bangalore... code reads the text file and count the occurrence of each word. import java.io.
finout longest word in sentence
finout longest word in sentence  write a program , how to find out the longest word in a sentence using java   Hi Friend, Try...; } } LongestWord(){ String word = ""; for(int i=0;i<stringArray.length;i++){ if(i==0
Ask java count
Ask java count  Good morning, I have a case where there are tables... | Java 1 | 10 | | b002 | beginner java | 5 | | b003 | advanced java book | 26 | | b004 | MySQL 1
count asterisk in a grid - Java Beginners
count asterisk in a grid  I need to write a program that uses a recursive method to count the number of asterisk in a square grid
PDF to Word Conversion - Java Beginners
PDF to Word Conversion  Hello, Can we convert a PDF document to Microsoft word document thru Java. If its not possible in Java, is it possible in any other language
Character count by while loop
Character count by while loop  Write the program to count the number...]; int count=0; for ( int i=0; i<third.length; i++){ if (ch==third[i]) count++; } boolean flag=false; for(int j=counter-1;j>=0;j--){ if(ch==third[j
java number to word - Java Beginners
java number to word  Can somebody please fix this to come out with the word, not the number 23? import java.util.Scanner; public class... { //----------------------------------------------------------------- // Pulls integers from a string to form a word
Scanning a word in a TEXT document......
by searching the JAVA and .net words present in the files..if JAVA word is present means...Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan
Scanning a word in a TEXT document......
by searching the JAVA and .net words present in the files..if JAVA word is present means...Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan
Count Row - JSP-Servlet
Count Row  Hello all, Please I need your help on how to desplay... using java servlet and html form. Thanks for your good job!   Hi friend... db = "register"; String driver = "com.mysql.jdbc.Driver"; int count= 0
from number to word
from number to word  i want to know weather there is any method that can be use in changing value from number to word. Example if i write ten thousand, it will automatically be written as 10000.   Java convert number

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.