Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Have Programming Question? Ask it here!
 
 
 


ESHWIN GEORGE
Java count occurrence of a word
1 Answer(s)      2 years 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.


Advertisement
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 Tutorials/Questions & Answers:
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
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
Advertisements
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 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
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
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 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 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
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 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
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
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 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
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
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
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
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
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
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
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
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
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
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
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
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
letter count problem - Java Beginners
letter count problem  i have a problem in my java coding to count two characters of a string. eg to count the letter "ou" in the string "How do you feel today?". The answer should be 5. but i still have error compiling
putting words to line number form a java file/test file and show occurrence
putting words to line number form a java file/test file and show occurrence  hi all i want putting words to line number form a java file and show occurrence but i cant use mapping method i can only use the LinkedList
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
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
How to merge two word document using java
How to merge two word document using java   how to merge two word document using java
java code to open and display MS-word document
java code to open and display MS-word document  java code to open and display MS-word document
Java code to convert pdf file to word file
Java code to convert pdf file to word file  How to convert pdf file to word file using Java
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
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
create MS Word in Java - Java Beginners
which creates MS Word in Java...create MS Word in Java  Hi, Could any one please post the code which creates the word document having different paragraphs and two tables. i have
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
Java program to generate the total count
Java program to generate the total count  I need a java program which would take a text file as an input and scans through it to generate the output. For Ex. : If the text file contains the following information: There is/are 3
Write a program in JAVA which accepts a sentence & displays the longest word in the sentence alongn with it length of the word.
Write a program in JAVA which accepts a sentence & displays the longest word in the sentence alongn with it length of the word.  **A program in JAVA which accepts a sentence & displays the longest word in the sentence along
Java word extraction program. - Java Beginners
Java word extraction program.  Need to code a public class named...- character word. The starting position should be based on the leftmost character... position and display the word on the console screen. Thanks
convert date month and year into word using java
;continue.... public void pass(int number) { int word, q...; if (word == 0) { q = number / 10...convert date month and year into word using java  convert the date
convert date month and year into word using java
;continue.... public void pass(int number) { int word, q...; if (word == 0) { q = number / 10...convert date month and year into word using java  convert the date
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
convert date month and year into word using java  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
Java Date Month Format  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
convert date month and year into word using java
convert date month and year  convert the date, month,year into word like 12-3-2012 into twelve march two thousands twelve pls send the code write in java.   Visit Here
 

 

 

DMCA.com