This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Count the String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Count the String" Watch "Count the String" New topic
Author

Count the String

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1372
I want to find out how many times String 'java' appeared in the particular file. How can i do that?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What have you tried? So far we don' know if you're stuck reading a file, finding the string "java", counting or showing the results.

Some places to look in the JavaDoc: File & FileReader, String.indexOf(), Pattern & Matcher. See if any of those get you going. If you get part way and get stuck, show us some code and we'll help keep you moving.

Take small steps - don't try to write the whole program in one sitting - and have fun!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

  • Open the file
  • Read each line
  • If you find "java" increment a counter


  • Which part are you unsure how to do?


    JavaRanch FAQ HowToAskQuestionsOnJavaRanch
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    But Paul wouldn't want you to forget to test with a line that says

    How about punctuation? Only you know your rules here. Maybe lines like:

     
    Don't get me started about those stupid light bulbs.
     
    subject: Count the String
     
    Similar Threads
    convert an array of String to char
    encoding and decoding in java
    Class definition
    Working with RDF
    convert String in to String array ???