Solved

How to read text file in java and count the no of repeated words?

Posted on 2011-02-25
12
1,706 Views
Last Modified: 2013-11-23
How to read text file in java and count the no of repeated words?

Regards,
Naveen.
0
Comment
Question by:naveenm_006
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 34985988

Do you mean you have certain words and you need to calculate their occurrence
in the text in the file?
0
 
LVL 92

Expert Comment

by:objects
ID: 34985993
you can use the following to read the words
http://helpdesk.objects.com.au/java/using-scanner-to-read-words-from-text-file

then use a Map<String, Integer> to store the word counts
0
 

Author Comment

by:naveenm_006
ID: 34985995
please find attached text file.
tokens.txt
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:naveenm_006
ID: 34986005
yes you are absolutely correct.Can give the sample code.
it's very urgent.

Regards,
Naveen

0
 

Author Comment

by:naveenm_006
ID: 34986007
it should read line by line.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 34986038
This should work, but I haven't yet tested it:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;

public class CountWords3 {

    public CountWords3(){


        ArrayList aa = new ArrayList();
        Hashtable h = new Hashtable();
        try {
            DataInputStream in = new DataInputStream(new FileInputStream("C:\\temp\\text.txt"));

            String buff;
            while((buff=in.readLine()) != null)
           {
                StringTokenizer t = new StringTokenizer(buff,",");
                      while(t.hasMoreTokens()){
                String s = t.nextToken().trim();
               if(!aa.contains(s))aa.add(t.nextToken());
                          if(h.get(s) != null){
                              Integer n = (Integer)h.get(s);
                              h.put(s, new Integer(n.intValue() +1));


                          }  else
                              h.put(s, new Integer(1));





            }

        }
            in.close();
        }catch(Exception ex) {
            System.out.println("Errorr");


    }
        for(int j=0; j<aa.size(); j++){
            String s = (String) aa.get(j);
            Integer n = (Integer) h.get(s);
            System.out.println(s + " " + n.intValue();

        }

    }
   public static void main(String [] args ){
       new CountWords3();
   }


}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 34986053
This is working and tested.
Reading from file c:\\temp\\test\\text5.txt


import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;

public class CountWords3 {

    public CountWords3(){


        ArrayList aa = new ArrayList();
        Hashtable h = new Hashtable();
        try {
            DataInputStream in = new DataInputStream(new FileInputStream("C:\\temp\\test\\text5.txt"));

            String buff;
            while((buff=in.readLine()) != null)
           {
                StringTokenizer t = new StringTokenizer(buff,",");
                      while(t.hasMoreTokens()){
                String s = t.nextToken().trim();
               if(!aa.contains(s))aa.add(s);
                          if(h.get(s) != null){
                              Integer n = (Integer)h.get(s);
                              h.put(s, new Integer(n.intValue() +1));


                          }  else {

                       //       System.out.println(" s" + s);
                              h.put(s, new Integer(1));
                          }





            }

        }
            in.close();
        }catch(Exception ex) {
            System.out.println("Errorr");


    }
        for(int j=0; j<aa.size(); j++){
            String s = (String) aa.get(j);
          //  System.out.println("ss  " + s);
            Integer n = (Integer) h.get(s);

            System.out.println(s + " " + n.intValue());

        }

    }
   public static void main(String [] args ){
       new CountWords3();
   }


}

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 34986055

Input:
amit,rajat,pankaj,ist,jagan,jordan,delhi
amit,delhi,japan,india,ist,riyad,new delhi
jaipur,ajmer,kashmir,jammu,kashmir,america
rajat,pankaj,trilok,faridabad,jaipur,delhi,abc
bcd,new delhi,jaipur,india,abc

Open in new window

output:

amit 2
rajat 2
pankaj 2
ist 2
jagan 1
jordan 1
delhi 3
japan 1
india 2
riyad 1
new delhi 2
jaipur 3
ajmer 1
kashmir 2
jammu 1
america 1
trilok 1
faridabad 1
abc 2
bcd 1

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34986349
Homework done then?
0
 
LVL 3

Expert Comment

by:greisch
ID: 35422996
for_yan has given a complete answer and should receive the points
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35423063
Thanks a lot, greisch, I really appreciate your
kind attention.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question