Map words to line number in text file and show occurence

Map words to line number in text file and show occurence

hi i want to Map words to line number in text file and show occurrence of word in java coding

View Answers

November 21, 2012 at 3:13 PM

Here is a code that reads the text file and count the occurrence of each word from the file and display it on the console.

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:
Map words to line number in text file and show occurence
Map words to line number in text file and show occurence  hi i want to Map words to line number in text file and show occurrence of word in java coding
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
Advertisements
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program  
line number in a file of entered string
line number in a file of entered string  when i entered string from console , it should show line number in a file of that string
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 number of most frequently occurring words  I have to: 1.Retrieve.... print: the total number of words processed, the number of unique words, the N
Reading Text file and storing in map
Reading Text file and storing in map  Hi I have multiple text files. I want to read thoses files and store those records in map. Map will be of "LinkedHashMap<String, Map<String, String>>" type. Please let me know
java program to insert data into a file and count the number of words from the file???????
java program to insert data into a file and count the number of words from the file???????  java program to insert data into a file and count the number of words from the file
Program to count the number of unique words in a file using HashMap
Program to count the number of unique words in a file using HashMap  ...()); System.out.println("The number of unique words: "+uniqueValues.size...[]) throws FileNotFoundException { File f = new File("C:/547647/word
How to write file text In New Line
you went new text in new line. For this we created a new text file named... to write file text in New line in Java...How to write file text In New Line  Hi friends, How to write file
Write Text To File In New Line.
Write Text To File In New Line. In this tutorial you will learn how to write text into file in a new line. When you are writing in a file you may be required to finish the line and want to write the other text in a new line
How to read a large text file line by line in java?
How to read a large text file line by line in java?  I have been... memory assigned is also a limit. So, we have decided to read the text file line... you kind advice and let's know how to read a large text file line by line in java
Java count words from file
Java count words from file In this section, you will learn how to determine the number of words present in the file. Explanation: Java has provides several... by using the StringTokenizer class, we can easily count the number of words
How to extract a specific line from a text file? - IoC
How to extract a specific line from a text file?  Hi all, i'm trying to write a code that return a specific line from a text file. so my first...° 100 to 120 for example. My Text file contain numbers and words. How i have
ModuleNotFoundError: No module named 'number-to-words'
ModuleNotFoundError: No module named 'number-to-words'  Hi, My... named 'number-to-words' How to remove the ModuleNotFoundError: No module named 'number-to-words' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'text-words'
ModuleNotFoundError: No module named 'text-words'  Hi, My Python... 'text-words' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-words python
ModuleNotFoundError: No module named 'text-words'
ModuleNotFoundError: No module named 'text-words'  Hi, My Python... 'text-words' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-words python
ModuleNotFoundError: No module named 'text-words'
ModuleNotFoundError: No module named 'text-words'  Hi, My Python... 'text-words' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-words python
java programming code to upload a dataset(text file) and show the values in graphically view
java programming code to upload a dataset(text file) and show the values in graphically view  how can be done upload a dataset(text file) and show the values in graphically view,,related to slam benchmarking java code
Java file new line
Java file new line In this section, you will learn how to write the text in new line of text file. Description of code: The package java.io.* has provide... a text in the file, there are different output streams. Among them
Convert Number To Words
Convert Number To Words       In this example, We are going to convert number to words. Code...) . It displays the string representing the number. Here is the code of this program
Display non-duplicate words from file
Display non-duplicate words from file In this tutorial, you will learn how to read a text file and display non-duplicate words in ascending order. The given... object is then passed to StringTokenizer which broke the text of file into words
Java Read File Line by Line - Java Tutorial
Java Read File Line by Line - Example code of reading the text file... class to Read text File Line by Line. Our example program is reading a text... how to read a text file in Java one line at a time: Video Tutorial
java script to replace words starting and ending with tild(~) symbol in a text area with the word highlighted yellow.
requirement we have to replace all the occurence of such words and replace with highlighted text. for example: The script should findout all the occurence of the words...java script to replace words starting and ending with tild(~) symbol in a text
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
Java Convert Number to Words
; } public void pass(int number) { int word, q; if (number < 10) { show(st1...) { q = number / 10; show(st4[q - 2]); } else { q = number / 10...Java Convert Number to Words In this section, you will learn how to convert
tO FIND UNIQUE WORDS IN A FILE USING HASHMAP
())); } System.out.println("Number of unique words in file "+cc...tO FIND UNIQUE WORDS IN A FILE USING HASHMAP  import java.util.... to be parsed * */ public void countWords(String text){ // remove any
Read text file in PySpark
Read text file in PySpark - How to read a text file in PySpark? The PySpark... text file and then collect the data into RDD. The term RDD stands... will create a text file with following text:ADS_TO_REPLACE_2 one two three
ModuleNotFoundError: No module named 'number-line'
ModuleNotFoundError: No module named 'number-line'  Hi, My Python... 'number-line' How to remove the ModuleNotFoundError: No module named 'number-line' error? Thanks   Hi, In your python environment
Line by Line reading from a file using Scanner Class
In this section, you will get to know about reading line by line a text file using java.util.Scanner class
Stoting file name in Map
Stoting file name in Map  how to store id and file name in map
Read Specific Line from file Using Java
Read Specific Line from file Using Java Here we are going to read a specific line from the text file. For this we have created a for loop to read lines 1 to 10 from the text file. If the loop reached fifth line, the br.readLine() method
Java read text file
a text file one line at a time. It can also be used to read large text files... the text file one line at a time. As it is not thread safe. Code must... text file in Java?": ADS_TO_REPLACE_3 Example of Read text File Line
nio read file line by line
nio read file line by line  Is there any way to read file line by line using the Java nio package? How to use the nio classes to read file line by line? Thanks   Hi, The easiest way to read file line by line is to use
Line Number Reader Example
Line Number Reader Example      .... In this example we are reading a file from system .Then we are reading number... class is provide getLineNumber() method to get the number of line. We
text file
text file  Hello can I modify the program below so that all the numerical data is stored in an external text file,that is the data contained in the array list.Thank you! mport java.util.*; import java.text.*; import
text file
text file  Hi can you help me I have to modify the program below so that all the data held in it is stored in an external text file.So there should... at the start of the program from a seperate external text file.Thank you! mport
Show Hide Text Javascript - Java Beginners
Show Hide Text Javascript  Please tell me how to show hide text using Java Script
Find the percentage of words which starts with a vowel in a file
Find the percentage of words which starts with a vowel in a file   Question: Find the percentage of words which starts with a vowel in a file. Ex: Input: Contents of File1- There is an endless loop in the program. Output: Vowel
ModuleNotFoundError: No module named 'text-game-map-maker'
ModuleNotFoundError: No module named 'text-game-map-maker'  Hi, My... named 'text-game-map-maker' How to remove the ModuleNotFoundError: No module named 'text-game-map-maker' error? Thanks   Hi
Java Write To File End Of Line
in the existing file will be written after the end of line i.e. new text...Java Write To File End Of Line In this tutorial you will learn how to write to file at the end of the line. Write to a file in which some contents
Count numbers of spaces and number of words from the input string
Count numbers of spaces and number of words from the input string Java In this section, you will learn how to count the number of spaces and words from... the number of spaces. Now, in order to count the number of words, we have splitted
Find number of words begin with the specified character
Count number of words begin with the specified character In this section, you will learn how to count the number of words that begin with the specified... with the input character. If found, counter will count the words. Here is the code
Java write to file line by line
Java write to file line by line In this section, you will learn how to write a file line by line. It is better to use Writer class instead of OutputStream class if you want to output text to the file since the purpose of Writer classes
java - read file line by line in Java
java - read file line by line in Java  Hi, how one can read a text file line by line in Java? What is the benefits of reading a file line by line in Java? Why I can't read a big text file in memory? Thanks   A text
ModuleNotFoundError: No module named 'nose-run-line-number'
ModuleNotFoundError: No module named 'nose-run-line-number'  Hi...: No module named 'nose-run-line-number' How to remove the ModuleNotFoundError: No module named 'nose-run-line-number' error? Thanks   Hi
Java read file line by line
are going to read a file line by line. For reading text from a file it's better... class is used to read text from a file line by line using it's readLine method...Java read file line by line In this section, you will learn how to read a file
Adding text in to text area of jframe at new line with different background colour
Adding text in to text area of jframe at new line with different background colour  Hi, I am reading text from text file and i want to write that text in to text area of jframe.But i am reading line by line from file and i want

Ads