Results 1 to 10 of 10

Thread: Counting words

  1. #1
    Wasley is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default Counting words

    Problem solved
    Last edited by Wasley; 02-01-2011 at 03:57 PM.

  2. #2
    syst3m.tr0jan is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Counting words is best done by using the StringTokenizer class..

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by syst3m.tr0jan View Post
    Counting words is best done by using the StringTokenizer class..
    Straight from the API documentation of this class:

    Quote Originally Posted by API StringTokenizer
    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    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 syst3m.tr0jan View Post
    Counting words is best done by using the StringTokenizer class..
    Which is somewhat being superseded by the String#split(...) method.

    And I think that the OP may just need a Scanner object to get the three words.
    Last edited by Fubarable; 01-30-2011 at 02:38 PM.

  5. #5
    syst3m.tr0jan is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    if we compare stringtokenizer and the split method of java.util.regex on stability issues.. can anyone elaborate on this one?

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

    Default

    Quote Originally Posted by syst3m.tr0jan View Post
    if we compare stringtokenizer and the split method of java.util.regex on stability issues.. can anyone elaborate on this one?
    Regular expressions were already here in the 70s of the last century; it's a very stable and matured technology. There are a few steps to implement: from Regular Expressions to NFAs (Non-deterministic Finit Automata) and (optionally) from NFA to DFA (Determinstic Finit Automata). There's nothing mysterious or rocket science about it; if Sun/Oracle managed to do it wrong I give them a funny face because they're amateurs then.

    kind regards,

    Jos
    Last edited by JosAH; 01-30-2011 at 03:13 PM.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    syst3m.tr0jan is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    actually i am new to java.. thats why i joined this forum to learn more much from you guys. thanks alot..

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

    Default

    Quote Originally Posted by syst3m.tr0jan View Post
    actually i am new to java.. thats why i joined this forum to learn more much from you guys. thanks alot..
    There's nothing wrong with being new to whatever; please always read the API documentation for the classes and/or methods you want to use. There's a lot of information in there. a.a.m.o.f. bookmark it together with Sun's/Oracle's tutorials, they are both a must read.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    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 JosAH View Post
    There's nothing wrong with being new to whatever; please always read the API documentation for the classes and/or methods you want to use. There's a lot of information in there. a.a.m.o.f. bookmark it together with Sun's/Oracle's tutorials, they are both a must read.
    Yup, and please try not to mislead other newbies by making pronouncements on what's best when you're not sure yourself.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Is there no other way around this. As I havent learnt how to use the StringTokenizer class
    did you not read the other replies that advised you not to use StringTokenizer and to use String.split or Scanner instead?

    If you know how many words are needed then you can use an array. If the number of words is unknown and to make it more flexible you can use a List.

    Java Code:
    line = line.replace(" ", "");
    That line of code is unnecessary as the Scanner.next method will return a String with no spaces in it.

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 04:03 PM
  2. Counting help
    By jksmithson in forum New To Java
    Replies: 1
    Last Post: 11-06-2009, 03:43 AM
  3. Counting characters
    By Tiff89 in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 10:21 AM
  4. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 02:33 PM
  5. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 06:51 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
  •