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



Home Answers Viewqa Java-Beginners Map words to line number in text file and show occurence

Have Programming Question? Ask it here!
 
 
 


Lam Wing Kai
Map words to line number in text file and show occurence
1 Answer(s)      a year and 9 months ago
Posted in : Java Beginners


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


Advertisement
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:
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
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
Advertisements
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
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
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
Convert Number To Words
Convert Number To Words       In this example, We are going to convert number to words. Code... the string representing the number. Here is the code of this program
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... write() adds the simple text to the file. Then the nextLine() writes a line
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... tutorial that explains you how to read a text file in Java one line at a time
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 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
Stoting file name in Map
Stoting file name in Map  how to store id and file name in map
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
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 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
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
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
objective c read file line by line
objective c read file line by line  Please explain how to read files in Objective C line by line
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
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
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
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
How to read a file line by line?
What is the benefit of reading a file file line by line? If you are developing... that can handle text file of any size. You never know about the file supplied... is to be developed. The best solution is to read the file one line a time
Java file line reader
Java file line reader In this section, you will learn how to read a particular line from file. Sometimes you want to read a particular line from the file... and parse the file path. Through the code, we want to read the line 3 of the file
program to display all words in the file in the sorted order without duplicates
program to display all words in the file in the sorted order without duplicates  I want a java program which accepts a file from user and displays all words in the file in the sorted order without duplicates
Show Hide Text Javascript - Java Beginners
Show Hide Text Javascript  Please tell me how to show hide text using Java Script
Layout text along a line using LineMetrics
Layout text along a line using LineMetrics... to layout text along a line. For this, class LineMetrics is used which layouts the text along a line. The Font class represents the font. The class
Java file line count
Java file line count In the section, you will learn how to count the number...("Number of Line: " + count); } } In this section, we have used data.txt file... if the file contains next line and the method nextLine() read the line of the string
Sorting text file
local website. i'm storing all file list in text file. And also adding a value to each line at the beginning. now i want to sort file content in descending order according to begin value. Text File having list like this: 5| 3| can anyone
Break a Line for text layout
Break a Line for text layout   ... to break a line. To break down the text into lines, the class LineBreakMeasurer... in the constructor of class AttributedString which holds the text. To allows iteration
Java read file line by line - Java Tutorial
ReadFile This is a text file? C:\nisha> This program reads the bytes from file and display it to the user. Download... input line by line with an appropriate BufferedReader
how to update the text file?
how to update the text file?  if my text file contains a string and integer in each line say,: aaa 200 bbb 500 ccc 400 i need a java code to update the integer value if my input String matches with the string in file. please
how to print a new line after a certain number of prints
how to print a new line after a certain number of prints  How do I make this program print the inputs the user gives 5 per line? So in other words I want the program to print 5 input numbers per line. this is my code so far
Show Transparent Text
Show Transparent Text       This section shows you the transparent text. To draw the text, Font class is used. To show transparency over the specified text, the class
How to extract the entire line with specific data from a Text in java?
How to extract the entire line with specific data from a Text in java? ... to extract only lines with number like 001 or 002 , every line with a number like in the example but not lines without number. The text content 500 lines but some
Show image and text on same jsp page
Show image and text on same jsp page  Hi all, I have to display image and text on the same jsp page. The text and image are both retrived from mysql... with text it is not possible. Please help me out. Thanks in advance
map
map  using map, i want display message based on id selected [both are strings]   i do no   Hi Friend, Try the following code...) { Map map=new HashMap(); map.put("Message1","Hello
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
JavaScript Count Words
to enter words of their choice. The javascript takes the value of the text area and using the regular expression, determine the number of words and finally display... that will display the number of the words in the textbox as the user enter the words
list file number - Java Beginners
list file number  sir you still haven't understand my problem. i have to list out only the number of files in a given directory,your solution still give number of (files+directories) in given directory. are jfilechooser provide
need to update key , value pair in map from properties file
need to update key , value pair in map from properties file  I have a map with key , value pair. with key as integer and value as string datatype. I want the keys and values to be dynamically updated into map. that is i have
Java Get Last Line of File
Java Get Last Line of File     ... you in understanding Get Last Line of File in Java . For this we have a class name "Get Last Line of File". Inside the main method we create an instance
Adding a text file - Java Beginners
Adding a text file  Hello, I need a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. The file contains nothing but strings
How to read a line from a doc file
How to read a line from a doc file  How to read a line from a doc file   Hi Friend, To run the following code, you need to download POI... void main(String[] args) { File file = null; WordExtractor extractor = null
Java file number of lines
Java file number of lines In this section, you will learn how to count the number of lines from the given file. Description of code: Java has provide... of Scanner class. The method hasNextLine() checks if the file contains next line
Read file from the Nth line
Read file from the Nth line Java provides several classes and methods to manipulate file. A file reading, is a common operation. Usually a file is to be read from top to bottom but here we are going  to read a file content from
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice... in my text file, if the item i entered matches with item in text file.how can i...]; for(int i=0;i File file ; FileReader fr=null ; FileWriter
Show text by button click using jQuery
Show text by button click using jQuery In this tutorial, we will discuss about hide/show text by clicking on button. In the below example, there are two buttons : hide and show .The "hide " button is used to hide text
Read Text file from Javascript - JSP-Servlet
Read Text file from Javascript  plz send the code How to Retrieve the data from .txt file thru Javascript? And how to find the perticular words in that file
Advertisements
 

 

 

DMCA.com