Re: Need help counting strings

From: Guillaume Jeudy (gjeudy_at_hotmail.com)
Date: 11/13/03


Date: 12 Nov 2003 19:07:53 -0800

theflystyle@yahoo.com (Ali) wrote in message news:<1e585462.0311121350.1ca50242@posting.google.com>...
> Still a little confused.. i was thinkin of how to use the following
> code::
>
> inStream = new BufferedReader(new FileReader(fileName));
> while((line3 = inStream.readLine()) !=null) {
> if(line2.indexOf(wanted) >=0)
> displayLine();
>
> and then somewhere insert a command that would take the result, which
> then would be counted to see how many times it was found.. or maybe im
> just looking in the wrong spot to insert the code..
>
> other then that i am still confused as so where to insert such code to
> count the result from the search..
>
> sorry guys but it seems as if i am a real noob...
>
> thanks for the help so far

Building on your snippet you could do the following,

int count = 0;
inStream = new BufferedReader(new FileReader(fileName));
        while((line3 = inStream.readLine()) !=null) {
             int curPos = 0;
             while((curPos = line3.indexOf(wanted)) >=0) {
                    count++;
                    line3 = line3.substring(0, curPos +
wanted.length());
             }
        }
        System.out.println("Found string " + wanted + ": " + count + "
times.");

I haven't tried to compile this little program but the goal is to lead
you towards one possible solution. The internal while loop is
necessary if you have the "wanted" pattern more than once on the same
line, this way you make sure you count every occurence. (readLine
reads data to the next linefeed in a text file)
Of course if you want the search to be case-insensitive you'll have to
refine further this snippet of code. I'll let you search for this
part..

Hope this helps,
Guillaume



Relevant Pages

  • Re: Size of a directory
    ... The responses so far have all been using command line methods. ... davjam@playing:~> ls -lR mnt ... As you can see, in this situation, mv creates the destination directory, ...
    (alt.os.linux.suse)
  • Re: Size of a directory
    ... The responses so far have all been using command line methods. ... davjam@playing:~> ls -lR mnt ... As you can see, in this situation, mv creates the destination directory, ...
    (alt.os.linux.suse)
  • Re: SHOW DEVICE/FILES with FORTRAN?
    ... :I have been looking around for a snippet of code to perform the following DCL ... willingness to generate version-dependent code. ...
    (comp.os.vms)
  • Re: SHOW DEVICE/FILES with FORTRAN?
    ... > I have been looking around for a snippet of code to perform the following DCL ... and walking the data structures to pull out some file ID's and IPID's and ...
    (comp.os.vms)
  • Re: Need help counting strings
    ... i was thinkin of how to use the following ... inStream = new BufferedReader); ... and then somewhere insert a command that would take the result, ... sorry guys but it seems as if i am a real noob... ...
    (comp.lang.java.help)