We help IT Professionals succeed at work.

find the occurance of word using java

mohammedf
mohammedf asked
on
342 Views
Last Modified: 2013-12-29
Hi

I have a file , ie HTML file , XML file .. .etc
i want a java method that calculates the number occurrence of a given term
ie:

public int method(String filename, String term)
return numberOfOcc

the term may be more than one word
the term may be in more than one line , at most two lines
ignore character cases
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Is this classwork?

Author

Commented:
nop

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
first read the file line by line

http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line

and for each line use the indexOf() String method to search for instances of term

Author

Commented:
indexOf or contains ??
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
objects , thanks it works
but why 'contains' worked not correctly ??

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
because you need to check if it appears more than once in the line

Author

Commented:
the program goes to infinite loop and it seems it don't break the while loop objects ???

Author

Commented:
here is the code

 public int calculateTermFreq(String part, String file) throws Exception {
        int cnt = 0;
        try {
            FileInputStream fstream = new FileInputStream("files//" + file);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String current = "";
            while ((current = br.readLine()) != null) {
                int start = 0;
                while (start >= 0) {
                    int index = current.indexOf(part, start);
                    if (index == -1) {
                        start = -1;
                    } else {
                        // found the term
                        cnt++;
                        start = index + part.length();
                        
                    }
                }
            }
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }
 
        return cnt;
 
    }

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
looks fine, try adding some debug logging to determine where it is getting stuck
whats your text file look like?

Author

Commented:
it is HTML file

CERTIFIED EXPERT
Top Expert 2016

Commented:
Neither html files nor xml files are line-oriented, so it doesn't make much sense to read them linewise.  Also, the phrase you're looking for could span more than one 'line'. Fortunately most html files at least are fairly small so it's best to read the file into a string to search. Line problems then disappear. See

http://www.technojeeves.com/joomla/index.php/free/93-file-to-string-in-java
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.