[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-02-25
12
Medium Priority
?
1,812 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

649 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