Read Text file and count occurences of certain string

Hello,
I have a text file with lines of data in it. I would like to read this text file and count how many lines match a certain string of text. 
For example my text file has this data in it.
dog,blue,big
dog,red,small
dog,blue,big
cat,blue,big
If the certain string of text is "dog,blue,big" then the count would return "2".
Thanks for your help

Hello,
Thank you for your post.
I am afraid that the issue is out of support range of VS General Question forum which mainly discusses the usage issue of Visual Studio IDE such as
WPF & SL designer, Visual Studio Guidance Automation Toolkit, Developer Documentation and Help System
and Visual Studio Editor.
I am moving your question to the moderator forum ("Where is the forum for..?"). The owner of the forum will direct you to a right forum.
In addition, if you are working with Windows Forms app. please consult on Windows Forms Forum:http://social.msdn.microsoft.com/Forums/windows/en-US/home?category=windowsforms
If you are working with WPF app, please consult on WPF forum:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wpf
If you are working with ASP.NET Web Application, I suggest that you can consult your issue on ASP.NET forum:
http://forums.asp.net/
 for better solution and support.
Visual Studio Language Forums:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=vslanguages
Best regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey.

Similar Messages

  • Reading a file, and counting how many e's there's in it!

    Hello everyone!
    Well, I'm trying to write a program that'll read a file and count the number of e's in there and print it out.
    This is what the Index.txt file contains:
    In Xanadu did Kubla Khan
    A stately pleasure-dome decree:
    Where Alph, the sacred river, ran
    Through caverns measureless to man
    Down to a sunless sea.And this is my source code:
    import java.io.*;
    public class Appearance{
        public static void main(String[] args) throws IOException{
         index = new BufferedReader(new FileReader(("Index.txt")));
            while((letter = index.readLine()) != null){
       if(letter.contains("e")){
           eCount++;
      System.out.print(eCount + "\n");
      if(index != null){
          index.close();
       index = null;
    private static BufferedReader index = null;
    private static String letter = null;
    private static int eCount = 0;
    }But for some reason when I run it, the output is 4?.. Please tell me why, as I figured that this should work.. :O.

    Vimsie wrote:
    But for some reason when I run it, the output is 4?.. Please tell me why, as I figured that this should work.. :O.Because you are counting the number of lines that contain one or more 'e's. You don't count the occurances.

  • Read text file and insert into MySQL

    Dears,
    I need to read text file and then insert the data in the correct column in the MySQL database
    example
    I have the following text file:
    field1=1234 field2=56789 field3=444555
    field1=1333 field2=2222 field3=333555
    and so on and so forth ,,note that all rows are identical and just the filed value is changed(there is a dilemeter between fields)
    how can I read field1,field2 and field3 from text file and insert them in the correct table and column in the database.....
    any help?????
    thanks for your cooperation
    Best Regars

    Sure.
    Which part don't you understand?
    1. Reading a text file
    2. Parsing the text file contents.
    3. Relational databases and SQL.
    4. How to create a database.
    5. How to connect to a database in Java.
    6. How to insert records into the database in Java.
    7. How to map Java objects to records in a database.
    This is a pretty nice list. Solve complex problems by breaking them into smaller ones.
    %

  • Reading text file and output (to stdout) a list of the unique words in the

    Hi,
    I have a main method as
    main.java
    package se.tmp;
    public class Main
    public static void main( String[] args )
    WordAnalyzer.parse( args[0] );
    and text file as
    words.txt
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the quick brown fox jumps over the lazy dog
    the requirement is like
    I need create this WordAnalyzer class, implement the parse method, and then commit the file. This method takes a single parameter, the filename of the file to parse. The method should read this file and output (to stdout) a list of the unique words in the file along with the number of times each appears in the file.
    Can anyone please help me on this?
    Thanks.

    Where are you having problems?

  • Reading text file and display in the selectOnechoice list item In ADF.

    Hi,
    I have a requirement to read the text field which have list of strings and that string display in the SelectOneChoice List item component on page load.
    I am using Jdeveloper 11.1.2.3 version.
    Any suggestion will highly appreciated..
    Thanks in advance.
    Regards

    Hi,
    Google will produce you with hints on how to read content of a file from Java (ideally the file uses some delimiter). Then in a managed bean, you read the file and save its content in a list of SelectItem. So your managed bean should have the following property and setter/getter pairs
    ArrayList<SelectItem> listFromFile = new ArrayList<SelectItem>();
    public void setListFromFile(ArrayList l){ //you don't need this }
    public ArrayList<SelectItem> getListFromFile(){
       //read file content and iterate over the file list entries
      for(i=0, i < fileContent.length, ++i){
         SelectItem si = new SelectItem();
         si.setValue(... the value to update the list of value with ...);
         si.setLabel("... the label to show in the list ...");
         listFromFile.add(si);
      return listFromFile;
    }The af:selectOneChoice component should look as follows
    <af:selectOneChoice id=".." value="...attribute to update with selection ..." ...>
       <f:selectItems value="#{managedBean.listFromFile}"/>
    </af:selectOneChoice>Frank

  • Reading text files and EOF

    I am attempting to read in blocks of data from a text file separated by a blank line. When the BufferedReader hits the first blank line it ends. I retyped the test text file and still cannot get it to continue to read the entire file; just the first block.
    fReader = new FileReader( textFile );
    bReader = new BufferedReader( fReader );
    line = bReader.readLine();
    while( line ! = null ){
    do stuff;
    bReader.close()
    The format of my textFile is:
    100
    some name
    type 1
    type 2
    101
    some other name
    type 1
    type 2
    Any suggestions would be greatly appreciated.

    Hi,
    I saw that BigDaddyLoveHandles already helped you a lot but I wanted to add a little something to that, because from what I understood you need to keep the blocks together.
    public static void main(String[] args) {
              BufferedReader theReader = null;
              try {
                   theReader = new BufferedReader(new FileReader("pathToYourFile"));
                   String line;
                   StringBuilder theBuilder = new StringBuilder();
                   while ((line = theReader.readLine()) != null){
                        if (line.isEmpty()){
                             doStuff(theBuilder.toString());
                             theBuilder = new StringBuilder();
                        }else {
                             theBuilder.append(line).append(System.getProperty("line.separator"));
                   doStuff(theBuilder.toString());
              } catch (IOException e) {
                   e.printStackTrace();
              } finally{
                   if (theReader != null)
                        try {
                             theReader.close();
                        } catch (IOException e) {
                             e.printStackTrace();
         }This actually allows your method doStuff to get the whole block vs. line by line.
    Regards

  • USING WEBUTIL TO READ TEXT FILE INTO TABLE HANGS AFTER CERTAIN NUMBER OF RE

    Dear
    when we use webutil to retrieve data from text file into database table
    (using text_io) it hangs after certain number of records ( approx. 1300
    records) while the total number of records to be inserted in the table exceeds
    12000 records while it works properly on forms6i with the normal text_io any
    help please...?
    thanks and regards

    WebUtil uploads the files as Binary - so yes you could have some issues if you have a Unix host - however, that would only mean that there is an extra character to trim off of the end of the line read by Text_io.

  • Read text file and split up.  How???

    Can anyone tell me how to split up a string into words.
    The string is: "me and my cat both like milk and cookies"
    I need read through the file and diplay the biggest word(s) and the smallest word(s).
    I would appriciate help on this as i am really stumped
    Thanks
    ..::M::..
    p.s. I also need to be able to do this in C++ but if i know it in java i think the C++ will be simple

    String[] words = "your sentencehere".split("\\s+");
    and now you have array of strings, each containigwords from text.
    this is helpful, is there a version in C++ tooThe short answer for C++ is "no". Unless of course you install one of the regexp libraries available. But there is no native language way to do it like in Java.
    However if they words always have spaces and always will have spaces you can write your C++ code like this:
    vector <string> words;
    ifstream inFile( "file.txt" );
    string temp;
    inFile >> temp;
    words.push_back( temp );
    PS I have not tested this code in anyway but it should be simple enough

  • How to read text file and store in database

    Respected All,
    I have text file on my computer.
    I am using forms6i and oracle 9i.
    I want that when i press button then specifice world in text file is read and dispaly on form to be stored in database.
    Please provide some solution.
    Kind Regards

    Hello,
    Study the functions of the TEXT_IO() package
    http://www.oracle.com/webapps/online-help/forms/10g/state?navSetId=_&navId=3&vtTopicFile=f1_help/oraini/c_text_io.html&vtTopicId=
    Francois

  • Reading a file and put it in a String

    I have a file and I want to put its rows in an array of strings
    I've done that
    File file = new File("./arguments/tabsArguments.txt");
    FileReader fileReader = new FileReader(file);
    String mystring;
    fileReader.read(mystring,0,20);
    for (int i=0;i<i_string.length();i++)
    tabs=i_string;
    fileReader.read(mystring,0,20);
    It throws this exception
    java.io.StreamCorruptedException: invalid stream header.
    I've done by myself the file "tabsArguments.txt"
    and it's a simple list of string.help please!

    Nothing in the code you posted can cause StreamCorruptedException to be thrown. Also, the code fragment you posted can't even be compiled because mystring is used before initialization, FileReader doesn't have a method with the signature read(String, int, int), and variable "tabs" is not defined. StreamCorruptedException is thrown "when control information that was read from an object stream violates internal consistency checks." This suggests that you use ObjectInputStream somewhere but I can't see where. http://java.sun.com/j2se/1.4.1/docs/api/java/io/StreamCorruptedException.html
    so from now on please post the actual code you have problems with...
    see http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html

  • Read the file and write the data into string or int

    Ok, here's another rookie question.
    I got to read the line from the file and store it in the memory as string or integer.
    I wrote this code:
    File inputFile = new File("c:/xxx.yyy");
    String outputString = new String();
    FileReader c1 = new FileReader(inputFile);
    StringWriter d1 = new StringWriter(outputString);
    int data;
             while((data = c1.read()) != -1)
                 d1.write(data);
                 System.out.println(data);
             c1.close();
             d1.close();This is how I see it, but it cannot be even compiled because there is no such thing as StringWriter(String) wich I find completely irrational. So tell me, am I retarded or what? No, but seriosly, please help the newbie to understand these streams, am I supposed to use some kind of buffer? Any help appreciated.

    A String is an immutable. You cannot update a String.
    A new String() is basically the same as an empty String ""
    An empty StringWriter can be created with new StringWriter().
    If you use a BufferedReader you can call readLine().
    BufferedReader in = new BufferedReader(new FileReader(filename));
    String line;
    while((line = in.readLine()) != null) {
      System.out.println(line);
    }

  • Find word in text file and count specific words

    now I'll try to explain what I need to do. I have file.txt file, It looks like:
    John //first line - name
    One
    Three
    Four
    Peter //first line - name
    Two
    Three
    Elisa //first line - name
    One
    Three
    Albert //first line - name
    One
    Three
    Four
    Nicole //first line - name
    Two
    FourSo I have program's code:
    public class Testing {
            public static void main(String args[]) throws Exception {
                Scanner input = new Scanner(System.in);
                System.out.println("Select word from list:");
                System.out.println();
                try {
                    FileReader fr = new FileReader("src/lt/kvk/i3_2/test/List.txt"); // this is list of words, everything all right here
                    BufferedReader br = new BufferedReader(fr);
                    String s;
                    while((s = br.readLine()) != null) {
                        System.out.println(s);
                    fr.close();
                    String stilius = input.nextLine();   // eneter word which I want to count in File.txt
                    BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/test/File.txt")); // from this file I need to count word which I entered before
                    int counter = 0;               
                    String line;
                    System.out.println("Looking for information");
                    while (( line = bf.readLine()) != null){
                        int indexfound = line.indexOf(stilius);
                        if (indexfound > -1) {
                             counter++;
                    if (counter > 0) {
                        System.out.println("Word are repeated "+ counter + "times");}
                        else {
                        System.out.println("Error...");
                    bf.close();
                catch (IOException e) {
                    System.out.println("Error:" + e.toString());
            }This program counting specific word (entered by keyboard) in file.txt.
    I need to make this program: for ex.: if I enter word: One It must show:
    Word One repeated 3 times by John, Elisa, AlbertAll what I need to elect by who this word repeated. But I don't know really how to make It, maybe LinkedList or I dont know, someone could help me?
    Thank you very much.

    966676 wrote:
    All what I need to elect by who this word repeated. But I don't know really how to make It, maybe LinkedListYou should choose the simplest type fullfilling your needs. In this case I'd go for <tt>HashSet</tt> or <tt>ArrayList</tt>.
    or I dont know, someone could help me?You need to introduce a variable to store the actual name which must be resetted if an empty line is found and then gets assigned the verry next word in the file.
    bye
    TPD

  • Reading from a text file and displaying the contents with in a frame

    Hi All,
    I am trying to read some data from a text file and display it on a AWT Frame. The contents of the text file are as follows:
    pcode1,pname1,price1,sname1,
    pcode2,pname2,price2,sname1,
    I am writing a method to read the contents from a text file and store them into a string by using FileInputStream and InputStreamReader.
    Now I am dividing the string(which has the contents of the text file) into tokens using the StringTokenizer class. The method is as show below
    void ReadTextFile()
                        FileInputStream fis=new FileInputStream(new File("nieman.txt"));
                         InputStreamReader isr=new InputStreamReader(fis);
                         char[] buf=new char[1024];
                         isr.read(buf,0,1024);
                         fstr=new String(buf);
                         String Tokenizer st=new StringTokenizer(fstr,",");
                         while(st.hasMoreTokens())
                                          pcode1=st.nextToken();
                               pname1=st.nextToken();
              price1=st.nextToken();
                              sname1=st.nextToken();
         } //close of while loop
                    } //close of methodHere goes my problem: I am unable to display the values of pcode1,pname1,price1,sname1 when I am trying to access them from outside the ReadTextFile method as they are storing "null" values . I want to create a persistent string variable so that I can access the contents of the string variable from anywhere with in the java file. Please provide your comments for this problem as early as possible.
    Thanks in advance.

    If pcode1,pname1,price1,sname1 are global variables, which I assume they are, then simply put the word static in front of them. That way, any class in your file can access those values by simply using this notation:
    MyClassName.pcode1;
    MyClassName.pname1;
    MyClassName.price1;
    MyClassName.sname1

  • How to read a text file and write text file

    Hello,
    I have a text file A look like this:
    0 0
    0 A B C
    1 B C D
    2 D G G
    10
    1 A R T
    2 T Y U
    3 G H J
    4 T H K
    20
    1 G H J
    2 G H J
    I want to always get rid of last letter and select only the first and last line and save it to another text file B. The output should like this
    0 A B
    2 D G
    1 A R
    4 T H
    1 G H
    2 G H
    I know how to read and write a text file, but how can I select the text when I am reading a text file. Can anyone give me an example?
    Thank you

    If the text file A look like that
    0 0
    0 3479563,41166 6756595,64723 78,31 1,#QNAN
    1 3479515,89803 6756588,20824 77,81 1,#QNAN
    2 3479502,91618 6756582,6984 77,94 1,#QNAN
    3 3479516,16334 6756507,11687 84,94 1,#QNAN
    4 3479519,14188 6756498,54413 85,67 1,#QNAN
    5 3479525,61721 6756493,89255 86,02 1,#QNAN
    6 3479649,5546 6756453,21824 89,57 1,#QNAN
    1 0
    0 3478762,36013 6755006,54907 54,8 1,#QNAN
    1 3478756,19538 6755078,16787 53,63 1,#QNAN
    2 0
    3 0
    N 0
    I want to read the line that before and after 1 0, 2 0, ...N 0 line to arraylist. I have programed the following code
    public ArrayList<String>save2;
    public BufferedWriter bufwriter;
    File writefile;
    String filepath, filecontent, read;
    String readStr = "" ;
    String[]temp = null;
    public String readfile(String path) {
    int i = 0;
    ArrayList<String> save = new ArrayList <String>();
    try {
    filepath = "D:\\thesis\\Material\\data\\CriticalNetwork\\test3.txt";
    File file = new File(filepath);
    FileReader fileread = new FileReader(file);
    BufferedReader bufread = new BufferedReader(fileread);
    this.read = null;
    // read text file and save each line content to arraylist
    while ((read = bufread.readLine()) != null ) {
    save.add(read);
    // split each arraylist[i] element by space and save it to String[]
    for(i=0; i< save.size();i++){
    this.temp = save.get(i).split(" ") ;
    // if String[] contain N 0 such as 0 0, 1 0, 2 0 then save its previous and next line from save arraylist to save2 arraylist
    if (temp.equals(i+"0")){
    this.save2.add(save.get(i));
    System.out.println(save2.get(i));
    } catch (Exception d) {
    System.out.println(d.getMessage());
    return readStr;
    My code has something wrong. It always printout null. Can anyone help me?
    Best Regards,
    Zhang

  • Reading text files in Java 1.4.2

    The program I've been working on for the past few weeks was written using the newest version of java, but I just found out yesterday that it has to be run with Java 1.4.2_10. Almost everything converted fine, my only problem being reading in a text file.
    Before I used a Scanner to read each line in a text file and store it in a String array, but with Scanners unavailable, I'm completely lost. Any suggestions?
    Here's my code for reading the file in, if this is at all helpful:
    Scanner input=new Scanner(new File(fileName));
    int counter=0;
    while ((input.hasNext())&&(counter<55000)) {  
         String line=input.nextLine();
         list[counter]=line;
         counter++;
    }

    DrLaszloJamf wrote:
    t.s wrote:
    Try this
    try {
    File toRead = new File("C://somefile.txt");
    BufferedReader reader = new BufferedReader(new FileReader(toRead));
    String line = null;
    while ((line = reader.readLine()) != null) {
    System.out.println(line);
    reader.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Why do people catch the FileNotFoundException exception separately?For me it's a personal preference, I don't think there is anything wrong with using just IOException. I think if that's the case, then there is nothing wrong with catching just "Exception".
    Edit: ...and I'm lazy. Eclipse does that for me ;)
    Edited by: t.s on Jun 18, 2008 8:19 AM

Maybe you are looking for

  • Problem with Exporting a Table with BLOB data.

    Hello All, I get error messages while exporting a table from Oracle8i ( 8.1.7.4.1), containing BLOB data: ORA-01555: snapshot too old: rollback segment number with name "" too small ORA-22924: snapshot too old Metalink says there is a bug in release

  • Application crashes at the start. Is the file damaged?

    When i try to open a file the application crashes. I think the file is damaged, isn't it? HELP

  • Why can't I use character styles in my shared content?

    When I try to use a character style in a text box I've identified with a Content Tag, the character style gets applied to the whole text box as a paragraph style would. I can't even manually make a word bold without it making the whole paragraph bold

  • Report History

    Dear All, When I've automatic updated an application on a few systems and run an application report, I found out that the new version shows up, but that also the old version is visible. This make's it hard to find out on which systems I still to inst

  • Use of bind variables in 'IN' operator

    how to use bind variable in the select stmt with 'in' operator?. like select * from table_name where col in (:id_list); I know we could use it for single value but how do i use bind variable for a list of values Thanks