Solved

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

Posted on 2011-02-25
12
1,583 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
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
Arrays.asList  VS  ArrayList 4 56
Which non-HTML GUI front end to use with Java? 3 22
Problem to Alipay 10 22
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

776 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