Results 1 to 9 of 9
  1. #1
    safecracker is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default homework help/ String Searcher

    Here is my assignment:
    This program asks for a sentence and a search word. It prints the number of times the search word appears exactly in the sentence, and the number of times the search word appears as part of another word.

    An example would be "Madam, I'm Adam" search word: adam.

    So far I've written the program so that it asks the user for the sentence and search word, and then converts everything to lower case. I'm having trouble with the counts though. I've been looking through the Lang book and have found a letter counter and trying to see if I can manipulate it to look like my search word instead, but so far have failed.

    Does anyone know how to count the number of times the word appears exactly and as part of another word? I'm hoping to get this finished for tomorrow morning.
    Last edited by safecracker; 12-03-2008 at 05:14 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    You can easily iterate through the sentence to check for a substring. Java provides a lot of tools in the String class that will help you out in achieving this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    emceenugget is right on the money. I'll try to give you a head start by posting a link to the String API. Read through this and you'll find several methods worth trying out:
    String (Java Platform SE 6)

  4. #4
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Default

    I think you might also want to take a look at StringTokenizer if you haven't already.
    miss.meli (-.-)zzZZ

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I don't think StringTokernizer is a good choice for this. Regular expressions are the best, in near future StringTokernizers can be deprecated. Actually we discuss about this several times in our community.

  6. #6
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    in near future StringTokernizers can be deprecated
    I'm new to java myself with only about a year and a half of experience :) , so I'm not to familiar with deprecated. What's that mean in programming?
    miss.meli (-.-)zzZZ

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In simple word, some method are not in implementation. Can cause several issues, like memory crash and so on. What Sun do is, they introduce new implements for that method and the existing one named as deprecated, means Suns' strongly to advice not to use and use the new one.

  8. #8
    miss.meli's Avatar
    miss.meli is offline Member
    Join Date
    Nov 2008
    Location
    Austin, TX
    Posts
    17
    Rep Power
    0

    Default

    Oh okay, that makes sense.
    miss.meli (-.-)zzZZ

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by miss.meli View Post
    Oh okay, that makes sense.
    Didn't you comes with such situations?

    Try show() on JFrame for setVisible() and see. But you should work on IDE, and different IDEs give different results/comments.

Similar Threads

  1. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 03:48 AM
  2. Homework help...
    By robrobrob in forum New To Java
    Replies: 4
    Last Post: 10-17-2008, 05:24 AM
  3. Need help with homework.
    By JavaNewbie0000 in forum New To Java
    Replies: 2
    Last Post: 07-31-2008, 04:06 PM
  4. thread homework
    By integralli in forum Threads and Synchronization
    Replies: 6
    Last Post: 06-21-2008, 05:34 PM
  5. Some input on a homework
    By alcohole4life in forum New To Java
    Replies: 1
    Last Post: 05-23-2008, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •