Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: word count in a in a file

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default word count in a in a file

    Hi
    Can anyone let me know how to count the number of words in a file for example "hello" in a file
    Thanks
    Raj


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: word count in a in a file

    Of course no one is going to give you the code for this, and I'm sure that you don't want us to, but rather you just want help on a step that you're stuck on. So, what have you tried and how is it not working? Or failing that, exactly what step has you stumped?

  3. #3
    Junior Member CosmicCode's Avatar
    Join Date
    Nov 2012
    Location
    KSA
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: word count in a in a file

    Hi,
    First of all, you just have to think about the problem and how to reach it. I'll give a hint using this questions.
    what separates words? [String s = "Hello World, my name is cosmicCode";].
    what separates these words in s ?

    hope you got the idea.

  4. #4
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    My Mood
    Sleepy
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: word count in a in a file

    Well, you want to make a string out a certain number of letters in the text file, for example you can check if it appears in one row by saving that row of text as let's say String s1 and compare it to a specified string (for example String s2= "hello"). Then you'd check if they are equal in something like:

     
     
     
    String s1;
    String s2 = "hello";
     
    int numberOfTimes; //Variable that contains the number of times s1 contains s2
     
    if(s1.containsIgnoreCase(s2)==true{ //If s1 contains s2, IgnoreCase is optional but handy if you don't want it to be case-sensitive
     
        numberOfTimes++;
     
    }

    And of course keep replacing s1 with the next set of letters, have luck!

Similar Threads

  1. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  2. Content in text file to MS Word file help!
    By ComputerSaysNo in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 27th, 2011, 11:39 AM
  3. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  4. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  5. MS word file in Oracle
    By tandonpiyush in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 07:41 AM