Results 1 to 14 of 14
  1. #1
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default a program that counts the number of each word in a sentence given by the user

    a program that counts the number of each word in a sentence given by the user The following is an example interaction with the program:



    Please enter a sentence: she likes cats and she likes dogs but he likes neither cats nor dogs



    Number of “she”: 2

    Number of “likes”: 3

    Number of “cats”: 2

    Number of “and”: 1

    Number of “dogs”: 2

    Number of “but”: 1

    Number of “he”: 1

    Number of “neither”: 1

    Number of “nor”: 1




    can you help me about this?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    A Map<String, Integer> can do it all. A simple String.split( ... ) separates the words in a String.

    kind regards,

    Jos

  3. #3
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    can i do it by using substring method?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rambo126 View Post
    can i do it by using substring method?
    Sure, if you want to do it the clumsy way ...

    kind regards,

    Jos

  5. #5
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    so how can i do can you help me a bit :)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rambo126 View Post
    so how can i do can you help me a bit :)
    I already did; given a String sentence you can easily find all the words in it by using the String split( ... ) method:

    Java Code:
    String[] words= sentence.split("\\s+");
    Given the array of words, all you have to do is add them to a Map<String, Integer> and update the Integer value each time you attempt to add a word. I've given it all away now ...

    kind regards,

    Jos

  7. #7
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    i did not want this way, how can i do by using substring method?

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by rambo126 View Post
    i did not want this way, how can i do by using substring method?
    I don't know; you have the idea of using the substring( ... ) method, not me so you should know what you have in mind ...

    kind regards,

    Jos

  9. #9
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    can anyone who knows substring method help me?

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

    Default

    You're going to have to manually split your String, if you must use subString. String has methods that will allow you to find the location of characters, such as the space character, ' '. These methods include indexOf(int ch) and indexOf(int ch, int fromIndex). Look these up in the API and you can use them to figure out how to extract each word form the String using subString. This will take a bit more effort on your part than if you used String#split(...).

    Much luck.

  11. #11
    rambo126 is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    could you clarify ? help me please

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

    Default

    Quote Originally Posted by rambo126 View Post
    could you clarify ?
    No problem. Please tell what needs clarification though. What do you understand, what don't you understand? Have you gone to the String API and examined the methods that have been mentioned? If so, what about them confuses you? Have you tried to create a small test program to try out these methods?

    help me please
    That's what we're trying to do.
    Last edited by Fubarable; 10-30-2010 at 10:17 PM.

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

    Default

    Quote Originally Posted by rambo127 View Post
    please write all code...
    Thread locked. Original poster private messaged.

    edit: rambo127 is not the OP, so post is now open and rambo127 has been banned for malicious posting.
    Last edited by Fubarable; 10-31-2010 at 07:08 PM.

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

Similar Threads

  1. Any Other Methods for Number to Word Conversion?
    By arnelcolar in forum New To Java
    Replies: 14
    Last Post: 04-14-2011, 05:45 PM
  2. Finding out number of Palindromes in a Sentence
    By rameshiit19 in forum New To Java
    Replies: 1
    Last Post: 09-13-2010, 04:44 AM
  3. Finding a word in a sentence?
    By blackrabbit in forum New To Java
    Replies: 6
    Last Post: 07-23-2010, 12:07 AM
  4. Replies: 4
    Last Post: 05-07-2010, 03:06 PM
  5. number to word with decimals
    By fertas in forum New To Java
    Replies: 3
    Last Post: 08-30-2008, 04:12 AM

Posting Permissions

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