How to count a occurence of a string in file

import java.util.*;
import java.io.*;
class Count
public static void main(String args[ ] )throws Exception
     FileReader fr=new FileReader("d:\\compfetch\\clean\\first.txt");
     BufferedReader br=new BufferedReader(fr);
     StringBuffer sb=new StringBuffer();
     while((s=br.readLine())!=null)
          sb.append(s);
     s=sb.toString();
Vector v= new Vector();
     v.addElement("job");
     v.addElement("jobs");
     v.addElement("career");
     v.addElement("careers");
     v.addElement("vacancy");
     v.addElement("vacancies");
     v.addElement("opportunity");
     v.addElement("openings");
     v.addElement("posting");
     v.addElement("postings");
     v.addElement("opportunities");
     v.addElement("placement");
     v.addElement("placements");
     v.addElement("job");
     v.addElement("job");
     Enumeration vEnum=v.elements();
     while(vEnum.hasMoreElements())
          System.out.println(vEnum.nextElement());
I need to count the occurence of each string given in the vector from the text file.I have the file and the vector.How do i compare the vector and textfile ,so that i can count the occurence of eachString in the vector with the text file.The text file may contain any of the words given in the vector.If found ,a count shld be made for each string .
regards,
koel

Try this code:
import java.util.*;
import java.io.*;
class Count
public static void main(String args[ ] )throws Exception
     FileReader     fr = new FileReader("c.c");
     BufferedReader br = new BufferedReader(fr);
     StringBuffer   sb = new StringBuffer();
     String         s;
     while((s=br.readLine())!=null)
          sb.append(s);
     s=sb.toString();
     Vector v= new Vector();
     v.addElement("job");
     v.addElement("jobs");
     v.addElement("career");
     v.addElement("careers");
     v.addElement("vacancy");
     v.addElement("vacancies");
     v.addElement("opportunity");
     v.addElement("openings");
     v.addElement("posting");
     v.addElement("postings");
     v.addElement("opportunities");
     v.addElement("placement");
     v.addElement("placements");
     Enumeration vEnum=v.elements();
     while(vEnum.hasMoreElements())
          String l = (String)vEnum.nextElement();
          int    a = 0;
          int    p = s.indexOf(l,a);
          int    c = 0;
          while (p != -1)
               c = c + 1;
               a = a + l.length() + p;
               p = s.indexOf(l,a);
          System.out.println(l+"  "+c);
}

Similar Messages

  • Count of occurences of a string senetence in 9i?

    Hi All,
    Is there an easy way using SQL to count the number of occrences of a string in a sentence? Dont want to use pl/sql if i can help it, maybe im missing an easy function?. Im using 9i rel2.
    eg find the count of ':' in the below string
    a:b:c:d: should return 4. (the separtor being : included on the end)
    a:b: should return 2
    and so on?
    Kind Regards
    Satnam

    SQL> SELECT a-LENGTH(REPLACE(SUBSTR(p,1,a),':',''))
      2    FROM
      3  (SELECT INSTR('a:b:c:d:e:',':',-1,1) a
      4         ,'a:b:c:d:e:' p
      5    FROM dual);
    A-LENGTH(REPLACE(SUBSTR(P,1,A),':',''))
                                          5Khurram

  • How to count number of methods in a java file

    Is there any way to know the count of methods or variables (instance) in a java file or at least in main() of a java file?
    If yes , can we know about their access modifiers?

    raj440 wrote:
    Is there any way to know the count of methods or variables (instance) in a java file or at least in main() of a java file?
    If yes , can we know about their access modifiers? It might be easier to use Reflection to interrogate the class itself instead of the source.
    A "variables (instance)" is somewhat of an oxymoron.

  • How to count the number or rows in file

    Hi Experts,
    I am file as source and Oracle as target. My file containing the some data which is going to load to the target.
    My requirement is that I want to count the number of rows which my file contains. Please help me how to count the total number of rows in file.
    Thx,
    Sahadeva.

    Hi,
    You won't give a name DEST_USER_NAME:ODI will get these information.
    it is important  Command on source and Command on Source side .
    -Pls write jython code on Command on Source side
    -And Command On Target side Choose technology as Oracle and set logical schema to HR_logical
    Sorry my mistake.İt is related jython syntax.Here is code:
    import java.lang as lang
    import java.sql as sql
    import java.lang.String
    import os
    lang.Class.forName("<%=snpRef.getInfo("DEST_JAVA_DRIVER")%>")
    cons=sql.DriverManager.getConnection("<%=snpRef.getInfo("DEST_JAVA_URL")%>", "<%=snpRef.getInfo("DEST_USER_NAME")%>", "<%=snpRef.getInfo("DEST_PASS")%>")
    dblinks= cons.createStatement()
    file1=open('EMP.txt','r')
    count=0
    line=file1.readline()
    while line !='':
         count+=1
         line=file1.readline()
    file1.close()
    sqlQuery = "insert into HR.ETL_FILE_LOG (FILE_COUNT) values (count)"
    rqQuery= dblinks.execute(sqlDBLink)
    cons.close()
    Regards

  • How to count number of lines in a file?

    I am using a BufferedReader to read my file, but I like to know how to count the number of lines in a file is there a way to do that in Buffered Reader or do I have to use FileInputLineStream?

    Well, if you know how to read one line with a Buffered reader, and you know how to keep reading lines until you run out, then all you have to to is initialize a counter to zero before you start reading, add 1 to it each time you read a line, and then examine its value when you're done.
    Try that, and post again if you get stuck.
    Just be careful how you construct your loop. "Off by one" errors are a common way to get bitten. It could happen if you count what you think is the last line, but you've actually already counted all the lines and you end up counting one extra for "there are no more lines".

  • How to count no. of pages in tif file

    Hi all,
    how to count the no. of pages in tiff file?

    Well google could tell you.
    The first hit on tif java pages gave me this link
    [http://java.sun.com/products/java-media/jai/iio.html|http://java.sun.com/products/java-media/jai/iio.html]
    On that page it has a desctiption of waht you want and even a link to a code sample..
    HTH,
    Lima

  • How to count number of occurences of a character  in a string

    I want to get the count of occurences of a character within a string. Is there any function for this or should i go for a PLSQL.
    For example is the sting is "occurences" & if i search for character "c", the answer should be 3.
    Regards,
    Sunil.

    SQL> select length('occurence') - length(replace('occurence','c')) from dual;
    LENGTH('OCCURENCE')-LENGTH(REPLACE('OCCURENCE','C'))
    3

  • How to find the number of occurence of a string

    String str="The AR tech seeks experienced candidates in java,jsp. The candidates should have atleast 1 year of experience in the specified skills.";
    here i need to find the number of occurence of the string "experience".
    how to find it out?

    String str="The AR tech seeks experienced candidates in java,jsp. The candidates should have atleast 1 year of experience in the specified skills.";
    String findStr = "experience";
    int count = 0;
    int index = 0;
    while(index != -1) {
    index = str.indexOf(findStr, (count == 0 ? 0 : index + findStr.length()));
    if(index != -1) {
    count++;

  • How to count number of repeated characters in a String

    I have a String.... 10022002202222.
    I need to know how many 2's are there in the string... here the string contains eight 2's..
    Thanks in advance..

    it is workingYes, but... attention to surprises...
    SQL> var v1 varchar2(1000);
    SQL> exec :v1 := 'How to count the number of occurences of a characters in a string';
    PL/SQL procedure successfully completed.
    SQL> select length(:v1) - length(replace(:v1,'c')) from dual;
    LENGTH(:V1)-LENGTH(REPLACE(:V1,'C'))
                                       6
    SQL> exec :v1 := 'cccccc';
    PL/SQL procedure successfully completed.
    SQL> select length(:v1) - length(replace(:v1,'c')) from dual;
    LENGTH(:V1)-LENGTH(REPLACE(:V1,'C'))
    SQL> select length(:v1) - nvl(length(replace(:v1,'c')),0) from dual;
    LENGTH(:V1)-NVL(LENGTH(REPLACE(:V1,'C')),0)
                                              6
    SQL>

  • Count the occurence of a character in a string

    Post Author: halfpat
    CA Forum: Formula
    Hello to everyone,
    I use CR 8,0. Is anyone can tell me if this is possible:
    I have a string "12345-C-1 23456-C-2 34567-C-4", how can I count the occurence of the "C" character in this string ?Is the InStr function can help me to do this ?
    I appreciate any suggestion,
    Thanks.

    Post Author: bettername
    CA Forum: Formula
    How about measuring the length of the string, and subtracting the length of the same string where C is replaced with <nothing>?
    len("12345-C-1 23456-C-2 34567-C-4") - len(replace("12345-C-1 23456-C-2 34567-C-4" , "C", ""))

  • How to replace multiple occurences of space in a string to a single space?

    How to replace multiple occurences of space in a string to a single space?

    Hi,
    try this code.
    data : string1(50) type c,
              flag(1) type c,
              dummy(50) type c,
              i type i,
              len type i.
    string1 = 'HI  READ    THIS'.
    len = strlen( string1 ).
    do len times.
    if string1+i(1) = ' '.
    flag = 'X'.
    else.
    if flag = 'X'.
    concatenate dummy string1+i(1) into dummy separated by space.
    clear flag.
    else.
    concatenate dummy string1+i(1) into dummy.
    endif.
    endif.
    i = i + 1.
    enddo.
    write : / string1.
    write : / dummy.

  • How to count no. of tokens in a string?

    hello everybody?
    I am using StringToknizer to read a csv file line by line.
    My problem is:
    i want to know how many tokens are there in a particular row. How to count it at a streatch?
    i am using
    <%
    if (st.hasMoreTokens())
         a3=st.nextToken().trim();
    %>i thought of using String.split(), but, i don't know how to use it?(what class to import)
    Anyguidence will be appreciated
    Thanks for your time.
    Regards,
    Ashvini

    StringTokenizer will do it for you!
    int numberOfTokens = st.countTokens();m

  • How to Count a Characteristic Occurence?

    Morning,
    I have Assembly(Material) being displayed as a Row item in my query. My need is to count the number of rows for Assembly in the query. I referred to "How to Count" doc, but that counts every occurence of assembly. Whereas, I need to count an Assembly just once.
    Can somebody help me on this?
    Thanks.

    Vijay,
    Here is how it is desired:
    Plant   Assembly  Count
    7835   711712101    1        
    <   >   711712103    1
    <   >   711712111    1
    <   >   711712401    1
    <   >   711713101    1
    Can you explain how I can do with this?

  • How do count the character in the given string?

    hi all,
    i have astring like 'AAAARAMARAOAAA'.
    i want count how many 'A's in this string using sql statement.

    Is that a joke? You asked that in a previous thread of yours: how do count how many 'A' s in this string using sql stmt?.
    C.

  • How do count how many 'A' s in this string using sql stmt?

    hi all,
    i have a string like 'AAAAARAMAAAAKRISHNAAAA'.
    in this ,i want to find out how many 'A's .
    how do find out this using select stmt.

    (length(s) - nvl(length(replace(s, 'A')), 0)) / length('A')but consider (by modifying the question slightly to "how do count how many 'AA' s in this string using sql stmt? ")
    SQL>  select (length('AAAAARAMAAAAKRISHNAAAA') - nvl(length(replace('AAAAARAMAAAAKRISHNAAAA', 'AA')), 0)) / length('AA') x from dual
             X
             6
    1 row selected.couldn't I argue that the result should be 10 as e.g. in
    SQL>  select * from xmltable('let $str := "AAAAARAMAAAAKRISHNAAAA"
                            let $search := "AA"
                            for $i in 1 to string-length($str)
                              where substring($str, $i, string-length($search)) = $search
                              return substring($str, $i, string-length($search))' columns x varchar2(10) path '.')
    X        
    AA       
    AA       
    AA       
    AA       
    AA       
    AA       
    AA       
    AA       
    AA       
    AA       
    10 rows selected.Matter of definition I suppose .... ;)
    btw. regexp_count also yields 6:
    SQL>  select regexp_count('AAAAARAMAAAAKRISHNAAAA', 'AA') x from dual
             X
             6
    1 row selected.

Maybe you are looking for

  • Dates Determination in Rebate

    Hi ERP gurus, I'm getting troubles with rebate management. Here below an example I created a rebate agreement starting from first of january and ending at the end of September. Let's assume that today 5th of october  i create a credit memo request th

  • Finding floating objects

    Is there someway to find floating objects in a Pages 09 document? I am trying to export to an epub and Pages 09 tells me that floating objects cannot be exported and to convert them to inline. I have many figures in the document most with captions wh

  • How can I uninstall adobe flash player?

    I was required to install it when I was going to upload something on metacafe but now that I have I can't do a lot of other flash things because of it and there is like no way to get rid of it, so how can I get rid of it because the other sites I use

  • How to download Premiere Elements 11? [was:hello]

    i need help downloading premier elements 11. I have already purchased a copy but had to delete it due to inability to startup.

  • User Display Settings Question

    Howdy! Display Colors are all out of whack (Almost looks like a photo negative) but only when logged on as a certain user. Is there a way to restore a users default display settings without changing other user settings? Also sRGB and RGB Color sync p