Counting times a word appears in document

I am trying to read from a file and count how many times a word appears in that document.
For example if i had a list of names
Daniel Fowler
Joe Mahogony
Daniel Peterson
Chris Sim
How would you count the amount of people named Daniel and display it?
I am currently using findInLine"Daniel" but when ran no luck.
Any ideas?

danieltnbaker wrote:
deepak,
the bufferedreader reads content from the file yes?
but what method would i use to count the amount of a word in that file?
finInLine finds the word but cannot turn that into an integer. any other methods?first of all the code I had posted in my previous post should work and it uses the Scanner to read the file.
If you want to use the FileReader approach the within the code that actually reads the file line by line, you can use the String.indexOf method (as suggested by nclow) to check if a particular word is present in that line of the file. IF it is there you increment a counter.
int counter = 0;
// Read through the entire file
String currentLineFromFile = bufferedReader.readLine(); // throws IOException
while(currentLineFromFile != null)
    // Add a carriage return (line break) to preserve the file formatting.
    textFromFile.append(carriageReturn + currentLineFromFile);
    currentLineFromFile = bufferedReader.readLine(); // throws IOException
      if (currentLineFromFile.indexOf("Daniel") != -1)
            counter++
      System.out.println("No of occurances- " + counter);
}hope this helps
[email protected]
Edited by: deepak_1your.com on 16-Apr-2008 14:18

Similar Messages

  • How much times a word appears in a string?

    There is any function to find out how much times a word
    appears in a string?
    For example:
    mystring = "Mytext... text... contentext";
    There is any function to tell me that "text" appears 3 times
    in that string?
    I searched for function like that, but didn't found
    one.

    here's a method that seem fairly consistent using the
    string.split() method - the returned array will always have one
    more index than the places in which the string was split.

  • Find the number of times a word appears

    In Google Chrome find/search (ctrl+F) it will tell you how many times the word you are finding appears.
    In Firefox I know you can find things (also ctrl+F) but is there any way in which you can find how many times that word appears, like in Chrome.

    Any help please, anyone?

  • Counting times a string appears inside another

    this is more of a request rather then a question , can anyone please post a regular expression that returns the times String key appeared in String content
    thanks .

    To the best of my knowledge there is no such animal.

  • How do I count the number of times a word appears in a column?

    What I have is a spreadsheet logging work history. Let's say the work location is "office", "home", or "travel" and I want to have a separate cell in another table for totals, count up the number of time each appears in the data for the year. So it would look like this:
    Date
    Location
    sept 1
    office
    sept 2
    home
    sept 3
    home
    sept 4
    travel
    sept 5
    office
    sept 6
    office
    sept 7
    office
    sept 8
    travel
    sept 9
    home
    sept 10
    office
    Totals
    Days
    10
    Office
    5
    Home
    3
    Travel
    2
    I just can't figure out how to develop the formula to put in the Totals column to make it work this way. If you can help me achieve this I'd really appreciate it!

    Hi SpartanAntarctican,
    Table 1 to record your locations
    Date
    Location
    sept 1
    office
    sept 2
    home
    sept 3
    home
    sept 4
    travel
    sept 5
    office
    sept 6
    office
    sept 7
    office
    sept 8
    travel
    sept 9
    home
    sept 10
    office
    Table 2 to sum your locations
    Totals
    Days
    10
    Office
    5
    Home
    3
    Travel
    2
    In Table 2 the formula in B2
    =ROWS(Table 1::A)−1
    Minus 1 because there is a Header Row in Table 1.
    In Table 2 the formula in B3 (and Fill Down)
    =COUNTIF(Table 1::B,A3)
    You can check this by adding 5 + 3 + 2 to arrive at 10
    Or maybe delete Row 2 put the Days calculation into a Footer Row in Table 2
    =SUM(B) .
    Regards,
    Ian.

  • Count occurence of words in string

    hi guys. i need some help.
    my problem is. i have a string, and want to know how many times each word appears in it, and print out only the word that appear more than 2 times.
    has any one an idea of how this is possible?

    There are many solutions, but here is the outline of one possible method.
    1) Split your string in words with the StringTokenizer class
    2) Create a HashMap
    3) look up your word in the hash map
    4a) If the word is not in the hashmap, create a new entry in the HashMap, where the key is your word from the original string, and the Value is (new Integer(1))
    4b) if the word is already in the hashmap, get the value from the hashmap, add one to it, then put the new Integer value in the hashmap
    5) for each value in the hashmap, print out the key , if the value is an Integer > 1
    With any luck, that will help you

  • HT5636 I was typing in a word for Mac document and I hit the apple key and the numeral 8 at the same time now all of my documents have these strange symbols please help

    I was typing in a word for Mac document and I hit the apple key and the numeral 8 at the same time now all of my documents have these strange symbols please help

    Simply type command 8 again. That toggles the "show paragraphs" function.

  • After installing lion and updating to pages 4.1 cannot view my document content after I open the document.  Shows how many pages I have and how many words in word count, but no words...

    after installing lion and updating to pages 4.1 cannot view my document content after I open the document.  Shows how many pages I have and how many words in word count, but no words...

    Same problem. Tried reinstalling Pages. Didn't help. The behavior is like I am typing with a white font on a white background, but I am not.

  • I am trying to export a pdf to word but the document keeps "loading " for a long period of time and won't complete?

    I am trying to export a pdf to word but the document keeps "loading " for a long period of time and won't complete?

    Hi Jennee,
    You're using the Save As > Microsoft Word command in Acrobat (and what version are you using)? At what point does the status bar in the lower-right corner hang up (Pre-processing, Recontructing, and so on)? What type of content is there in the PDF, and how large is it? Are you having Acrobat run OCR on the file as it converts (you can check this by clicking Settings in the Save As dialog box)? Do you have trouble saving other PDF files as Word documents?
    A lot of questions, I know. But I need to know a bit more about the document to determine why it's hanging up on export.
    Thanks
    Sara

  • It used to be that when I created a pdf from a word or publisher document, hyperlinks worked fine. Now I cannot get them to work unless the text displayed for the link is the complete address and it all appears on one line. Why?

    it used to be that when I created a pdf from a word or publisher document, hyperlinks worked fine. Now I cannot get them to work unless the text displayed for the link is the complete address and it all appears on one line. Why?

    You can print to the Adobe PDF printer, but that only creates links that Acrobat or Reader recognize as such. That is the reason for needing the entire URL on one line. It is not actually an embedded link, but one created by Acrobat on the fly. With OFFICE 2013 you have two choices: 1. create the PDF with the print to Adobe PDF and then add the links in Acrobat so that they are complete or 2. I think the WORD save as PDF or XPS will create the PDF with links, but not sure. If so, that would be the quickest solution for you, but be aware of potential problems with the MS plugin.
    Of course, the third option is to upgrade to XI which can be done with AA 9. You will probably not be eligible for a for an upgrade to whatever the next version will be.

  • 3?'s: Message today warning lack of memory when using Word (files in Documents) something about "idisc not working" 2. Message week ago "Files not being backed up to Time Capsule"; 3. When using Mac Mail I'm prompted for password but none work TKS - J

    3 ?'s:
    1  Message today warning lack of memory when using Word (files in Documents) something about "idisc not working"
    2. Message week ago "Files not being backed up to Time Capsule";                                                                                                                                             
    3. When using Mac Mail I'm prompted for password but none work
    Thanks - J

    Thanks Allan for your quick response to my amateur questions.
    Allan:     I'm running version Mac OS X Version 10.6.8     PS Processor is 2.4 GHz Intel core 15 
    Memory  4 gb  1067   MHz  DDr3  TN And @ 1983-2011 Apple Inc.
    I just "Updated Software" as prompted.
    Thanks for helping me!    - John Garrett
    PS.
    Hardware Overview:
      Model Name:          MacBook Pro
      Model Identifier:          MacBookPro6,2
      Processor Name:          Intel Core i5
      Processor Speed:          2.4 GHz
      Number Of Processors:          1
      Total Number Of Cores:          2
      L2 Cache (per core):          256 KB
      L3 Cache:          3 MB
      Memory:          4 GB
      Processor Interconnect Speed:          4.8 GT/s
      Boot ROM Version:          MBP61.0057.B0C
      SMC Version (system):          1.58f17
      Serial Number (system):          W8*****AGU
      Hardware UUID:          *****
      Sudden Motion Sensor:
      State:          Enabled
    <Edited By Host>

  • HT6084 Since upgrading to OS X Mavericks I have not been able to work on any MS Word tracked changes documents (works fine if no tracked changes), as every time upon opening such a word file the word processor closes / crashes. What can be done in this ca

    Since upgrading to OS X Mavericks I have not been able to work on any MS Word tracked changes documents (works fine if no tracked changes or comments in the doc). Every time upon opening such a word file with tr.changes the word processor closes / crashes.
    What can be done in this case?

    I just discovered that by turning off in tracked changes balloons in Word preferences prevents the software from crashing, but from then on I need to use only the review pane.

  • I have been downloading pictures and they open up in Preview which is fine but I downloaded a bunch of them and now every time I open preview, ALL those pictures open up. This also happens with MS word - the last document open automatically opens. HELP!

    I have been downloading pictures and they open up in Preview which is fine but I downloaded a bunch of them and now every time I open preview, ALL those pictures open up. This also happens with MS word - the last document open automatically opens. HELP!

    It's Resume, a new Lion feature, which can be turned off here:
    Regards,
    Colin R.

  • Returning Count of Matching Words Rather than Score

    I'm using the SCORE function that will return a number representing the relevance of the matching search result; however, what the users really want is a COUNT of the number of search results returned within the document. For a search on the word "cat" this would return the number of times the word "cat" appears in the document. Ideally, if the user searched for "(dog and cat) within sentence", the COUNT would be of the number of times both dog and cat occur within sentences in the document; however, it would be acceptable to count the number of matching words in the document individually.
    Does Oracle Text have any way to return this COUNT directly within the results Oracle Text query? I can use a CTX_DOC.MARKUP call to mark the document up and the count the instances of the markup strings, but that's going to be a lot slower than if Oracle supports this out-of-the-box on the original query syntax.

    Yes (in 10g onwards), you need to use a query template with <score algorithm="COUNT"/>
    See here:
    http://www.oracle.com/technology/products/text/x/10g_tech_overview.html#qry_cscore
    Though the example there is a bit confusing since it has algorithm="SCORE" - the option for normal scoring.

  • Count space in word

    hi how can i count alphabet in a letter for example if i have word simple how can i write query to show that simple got 5 alphabat and count the space if its there

    It would help if you were clear in what you want. Please read: {message:id=9360002}
    Here's an example of the sort of thing you could do if you wanted...
    SQL> drop table t
      2  /
    Table dropped.
    SQL>
    SQL> create table t as
      2    select 'SUPERCALAFRAJALISTICEXPIALIDOCIOUS' as txt from dual union all
      3    select 'THISISMYVERYLONGNAME' from dual union all
      4    select 'SIMPLE 123' from dual
      5  /
    Table created.
    SQL> with chrpos as (select t.txt, SUBSTR(t.txt,x.rn,1) as chr, x.rn as pos
      2                  from   (select rownum rn from dual,t connect by rownum <= length(t.txt)) x, t)
      3  select txt, chr, cnt, ltrim(sys_connect_by_path(pos,','),',') as positions
      4  from (
      5        select c.txt, c.chr, c.cnt, c2.pos, row_number() over (partition by c.txt, c.chr order by c2.pos) as rn
      6        from (
      7              select c1.txt, c1.chr, count(*) cnt
      8              from chrpos c1
      9              group by txt, chr
    10             ) c,
    11             chrpos c2
    12        where c2.txt = c.txt
    13        and   c2.chr = c.chr
    14       )
    15  where connect_by_isleaf = 1
    16  connect by txt = prior txt and chr = prior chr and rn = prior rn+1
    17  start with rn = 1
    18  order by txt, chr
    19  /
    TXT                                C        CNT POSITIONS
    SIMPLE 123                                    1 7
    SIMPLE 123                         1          1 8
    SIMPLE 123                         2          1 9
    SIMPLE 123                         3          1 10
    SIMPLE 123                         E          1 6
    SIMPLE 123                         I          1 2
    SIMPLE 123                         L          1 5
    SIMPLE 123                         M          1 3
    SIMPLE 123                         P          1 4
    SIMPLE 123                         S          1 1
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS A          5 7,9,12,14,25
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS C          3 6,20,30
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS D          1 28
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS E          2 4,21
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS F          1 10
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS I          5 16,19,24,27,31
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS J          1 13
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS L          3 8,15,26
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS O          2 29,32
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS P          2 3,23
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS R          2 5,11
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS S          3 1,17,34
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS T          1 18
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS U          2 2,33
    SUPERCALAFRAJALISTICEXPIALIDOCIOUS X          1 22
    THISISMYVERYLONGNAME               A          1 18
    THISISMYVERYLONGNAME               E          2 10,20
    THISISMYVERYLONGNAME               G          1 16
    THISISMYVERYLONGNAME               H          1 2
    THISISMYVERYLONGNAME               I          2 3,5
    THISISMYVERYLONGNAME               L          1 13
    THISISMYVERYLONGNAME               M          2 7,19
    THISISMYVERYLONGNAME               N          2 15,17
    THISISMYVERYLONGNAME               O          1 14
    THISISMYVERYLONGNAME               R          1 11
    THISISMYVERYLONGNAME               S          2 4,6
    THISISMYVERYLONGNAME               T          1 1
    THISISMYVERYLONGNAME               V          1 9
    THISISMYVERYLONGNAME               Y          2 8,12
    39 rows selected.Which details each individual character of the string, indicating a count of how many times that character appears in the string, and lists all the positions of that character in the string.

Maybe you are looking for

  • Check if a component exists in document thanks to its ID

    Hi ! I m using a flex app which communicates with AS/400 server thanks to HTTP Service. I'm checking forms with an RPG program which sends me an xml result with the name of incorrect fields. I give you an exemple myResultXML:XML = result returned by

  • Please designate the path of new firmware

     This message keeps poping up ("Please designate the path of new firmware") when I try to upgrade the firmware of my WRT110 wireless router, even though I used the BROWSE button to select the new firmware .bin file. Anybody has a clue to resolve this

  • Search everywhere first?

    I know this has probably been brought up but to try to find previous posts about it exemplifies the actual problem! Why does the initial search in any forum search all the forums first?  For this post I tried to search for words such as "search" and

  • "Can't get folder 'a'" Error with Applescript

    Here is my complete applescript application. I am supposed to select a series folder, and it goes into the folder and for each season folder does something inside, including grabbing the name of the season folder. But it keep throwing an error on the

  • Quicktime won't start on Win XP

    All, I had a working version of Quicktime and iTunes on my XP, and everything worked until I recently wanted to start iTunes, and noticed it did show up as a process for 2 seconds but then disappeared silently. The same goes for Quicktime; I can't st