Re: Need help counting strings
From: Guillaume Jeudy (gjeudy_at_hotmail.com)
Date: 11/13/03
- Next message: Michael Dunn: "Re: Need help counting strings"
- Previous message: Matt Humphrey: "Re: most efficient collection for.."
- In reply to: Ali: "Re: Need help counting strings"
- Next in thread: Michael Dunn: "Re: Need help counting strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Michael Dunn: "Re: Need help counting strings"
- Previous message: Matt Humphrey: "Re: most efficient collection for.."
- In reply to: Ali: "Re: Need help counting strings"
- Next in thread: Michael Dunn: "Re: Need help counting strings"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|