Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Count the String

 
kri shan
Ranch Hand
Posts: 1462
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Open the file
  • Read each line
  • If you find "java" increment a counter


  • Which part are you unsure how to do?
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic