Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

counting no. of occurances of a word in text file using java IO  RSS feed

 
Mohan Mehra
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want the code for printing the number of occurances of a particular word in text file. for example there is file called one.txt and it has many words, say i just want to print how many times the word london has occured in the file. also i want to replace london with new york say. how do i do this?
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are NotACodeMill - you won't get full working programs here. We are willing to help you out though.

Do you already know how to read text from a file? If so, do you know how to separate that text into words?
 
Mohan Mehra
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags to your code to make it easier to read. Your code depends on the method sop(), which you don't show. I'm assuming it's shorthand System.out.println(). That, and the misspelled, mis-cased "Fileread" means your code won't even compile. If it did, it would just print out the file's characters one by one. I would use the Character.isWhitespace() to identify word divisions. Try fixing the compile errors, and adding something with the whitespace check and see how far that gets you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!