• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

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

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Marshal
Posts: 8612
623
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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";?
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 9831
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic