Solved

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

Posted on 2011-02-25
12
1,540 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
12 Comments
 
LVL 47

Expert Comment

by:for_yan
Comment Utility

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
Comment Utility
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
Comment Utility
please find attached text file.
tokens.txt
0
 

Author Comment

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

Regards,
Naveen

0
 

Author Comment

by:naveenm_006
Comment Utility
it should read line by line.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
Homework done then?
0
 
LVL 3

Expert Comment

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

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now