Solved

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

Posted on 2011-02-25
12
1,657 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 68
jsp error 6 42
Setup GlassFish 4 34
restrict decimal places for double datatype 10 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

679 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