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

How can I improve this code? Returns a String, a sentence containing a specific keyword.  RSS feed

 
Eric Matthew
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't homework or anything school related, just a small part of a personal project of mine. My main personal project searches given URLs and other text files looking for specific key words, keeps a count on how many times it was found and stores the values in a map object. This, soon to be a method, took longer than I expected or maybe I'm just really tired. Open to any improvements or criticism.

My intentions with this code is just to simply return a String, a sentence in which the specific key word was found, and print it out or perhaps store it in a List and print it later..
 
Liutauras Vilda
Marshal
Posts: 4638
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Eric,

What should return after typed phrase "dog and"?
What should return after typed phrase "The dog"; "the dog";?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do this with regexes, but let's see if you can work out the problems Liutauras pointed out first.
 
Carey Brown
Bartender
Posts: 2993
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The approach I would take is to break the problem down into two steps: 1) parse sentences, and 2) look for keywords in each sentence. This would probably be slightly slower than your code but then it would be easier to handle the special cases without making a mess of the code.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:The approach I would take is to break the problem down into two steps: 1) parse sentences...

I agree in general with your approach, but then a major question becomes: What is a sentence?

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!