StringTokenizer

Discussion in 'Java' started by Anony!, Jul 27, 2004.

  1. Anony!

    Anony! Guest

    Hi

    How to read a string, a substring at a time.

    The StringTokenizer class only breaks strings by 3 delimiters: tab, space,
    comma.

    consider

    StringTokenizer tokeinzer = new StringTokenizer(mystring);

    while (tokenizer.hasMoreTokens())
    {
    //break string into substrings of 5 characters
    }

    Thanks in advance

    AAa
    Anony!, Jul 27, 2004
    #1
    1. Advertising

  2. Anony!

    jAnO! Guest

    "Anony!" <> wrote in message
    news:M7rNc.18954$...
    > Hi
    > How to read a string, a substring at a time.
    > The StringTokenizer class only breaks strings by 3 delimiters: tab,

    space,
    > comma.
    > consider
    > StringTokenizer tokeinzer = new StringTokenizer(mystring);
    > while (tokenizer.hasMoreTokens())
    > {
    > //break string into substrings of 5 characters
    > }


    from SUN:

    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



    --
    No matter what happens, somebody will find a way to take it too
    seriously.
    jAnO!, Jul 27, 2004
    #2
    1. Advertising

  3. Anony!

    Anony! Guest

    "VisionSet" <> wrote in message
    news:disNc.377$...
    >
    >
    > "Anony!" <> wrote in message
    > news:M7rNc.18954$...
    > > Hi
    > >
    > > How to read a string, a substring at a time.
    > >
    > > The StringTokenizer class only breaks strings by 3 delimiters: tab,

    space,
    > > comma.

    >
    > No, you can supply any delimiters you want.


    I tried to figure out how to set a delimiter of 5 characters. Don't think
    such as a thing exist for delimiters.

    I may have to go java.io or something

    aAa
    Anony!, Jul 27, 2004
    #3
  4. Anony!

    VisionSet Guest

    "Anony!" <> wrote in message
    news:M7rNc.18954$...
    > Hi
    >
    > How to read a string, a substring at a time.
    >
    > The StringTokenizer class only breaks strings by 3 delimiters: tab, space,
    > comma.


    No, you can supply any delimiters you want.

    --

    Mike W
    VisionSet, Jul 27, 2004
    #4
  5. Anony!

    Alan Moore Guest

    On Tue, 27 Jul 2004 13:48:59 +0200, "jAnO!"
    <> wrote:

    >"Anony!" <> wrote in message
    >news:M7rNc.18954$...
    >> Hi
    >> How to read a string, a substring at a time.
    >> The StringTokenizer class only breaks strings by 3 delimiters: tab,

    >space,
    >> comma.
    >> consider
    >> StringTokenizer tokeinzer = new StringTokenizer(mystring);
    >> while (tokenizer.hasMoreTokens())
    >> {
    >> //break string into substrings of 5 characters
    >> }

    >
    >from SUN:
    >
    >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


    split() is not approriate to this task, either. If you just want to
    break the string into five-character chunks, use substring().
    Alan Moore, Jul 27, 2004
    #5
  6. Anony!

    Diego Guest

    In article <ZvrNc.18984$>,
    says...
    >
    > "VisionSet" <> wrote in message
    > news:disNc.377$...
    > >
    > >
    > > "Anony!" <> wrote in message
    > > news:M7rNc.18954$...
    > > > Hi
    > > >
    > > > How to read a string, a substring at a time.
    > > >
    > > > The StringTokenizer class only breaks strings by 3 delimiters: tab,

    > space,
    > > > comma.

    > >
    > > No, you can supply any delimiters you want.

    >
    > I tried to figure out how to set a delimiter of 5 characters. Don't think
    > such as a thing exist for delimiters.
    >
    > I may have to go java.io or something
    >
    > aAa
    >
    >
    >



    As said, you can supply any delimiters.

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/StringTokenizer.html
    Diego, Jul 27, 2004
    #6
  7. Anony!

    BADBOY Guest

    As said in other replys u can set any delimiters eg-: new
    StringTokenizer(mystring,"a,b,c,d",true);
    the boolean specifys whether or not the delimiters are returned as tokens.
    Although i dont think StringTokenizer will do what ure expecting.
    You could use :- char[] cArray=mystring.toCharArray();
    then cast the first 5 chars back into a string, then the next 5 chars, then
    the next etc...

    "Anony!" <> wrote in message
    news:M7rNc.18954$...
    > Hi
    >
    > How to read a string, a substring at a time.
    >
    > The StringTokenizer class only breaks strings by 3 delimiters: tab, space,
    > comma.
    >
    > consider
    >
    > StringTokenizer tokeinzer = new StringTokenizer(mystring);
    >
    > while (tokenizer.hasMoreTokens())
    > {
    > //break string into substrings of 5 characters
    > }
    >
    > Thanks in advance
    >
    > AAa
    >
    >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.726 / Virus Database: 481 - Release Date: 22/07/2004
    BADBOY, Jul 27, 2004
    #7
  8. Anony!

    Oscar kind Guest

    Anony! <> wrote:
    > "VisionSet" <> wrote in message
    > news:disNc.377$...


    [Discussion on java.util.StringTokenizer]

    >> No, you can supply any delimiters you want.

    >
    > I tried to figure out how to set a delimiter of 5 characters. Don't think
    > such as a thing exist for delimiters.


    Yes, there is. For example the 5 charrcters '1', '2', '3', '4' and '5':

    StringTokenizer tokenizer = new StringTokenizer ("test", "12345");


    > I may have to go java.io or something


    If you want to split a String into chucks of 5 characters, just use
    String#substring(int) or String#substring(int, int), as pointed out by
    Alan Moore. The java.io package is overkill, unless you were using it to
    read the String's anyway.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Jul 27, 2004
    #8
  9. Anony!

    Anony! Guest

    lets see i have a string

    String mystring = "asdfergghjukluipfkjg";

    i want to break it up into:

    asdfe
    rgghj
    uklui
    pfkjg

    using this:

    StringTokenizer(mystring,"a,b,c,d,e",true);

    as u can see the first 5 characters dont match the pattern abcde

    Another problem is that i cant use substring(startIndex, endIndex) becuase
    that would be hardcoding it. I want to be able to pass the string through a
    command-line argument and break the string into substrings of 5 characters
    in length. I didn't think it would be so damn difficult.

    Thanks anyway
    AaA



    "BADBOY" <> wrote in message
    news:yswNc.516$...
    > As said in other replys u can set any delimiters eg-: new
    > StringTokenizer(mystring,"a,b,c,d",true);
    > the boolean specifys whether or not the delimiters are returned as tokens.
    > Although i dont think StringTokenizer will do what ure expecting.
    > You could use :- char[] cArray=mystring.toCharArray();
    > then cast the first 5 chars back into a string, then the next 5 chars,

    then
    > the next etc...
    >
    > "Anony!" <> wrote in message
    > news:M7rNc.18954$...
    > > Hi
    > >
    > > How to read a string, a substring at a time.
    > >
    > > The StringTokenizer class only breaks strings by 3 delimiters: tab,

    space,
    > > comma.
    > >
    > > consider
    > >
    > > StringTokenizer tokeinzer = new StringTokenizer(mystring);
    > >
    > > while (tokenizer.hasMoreTokens())
    > > {
    > > //break string into substrings of 5 characters
    > > }
    > >
    > > Thanks in advance
    > >
    > > AAa
    > >
    > >

    >
    >
    > ---
    > Outgoing mail is certified Virus Free.
    > Checked by AVG anti-virus system (http://www.grisoft.com).
    > Version: 6.0.726 / Virus Database: 481 - Release Date: 22/07/2004
    >
    >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.726 / Virus Database: 481 - Release Date: 22/07/2004
    Anony!, Jul 28, 2004
    #9
  10. Anony!

    P.Hill Guest

    Anony! wrote:

    > lets see i have a string
    >
    > String mystring = "asdfergghjukluipfkjg";
    >
    > i want to break it up into:
    >
    > asdfe
    > rgghj
    > uklui
    > pfkjg
    >
    > using this:
    >
    > StringTokenizer(mystring,"a,b,c,d,e",true);


    Sorry, but you need to restate your problem in a way that makes sense.
    Your declaration of StringTokenizer will break any String whenever
    it finds a "a" OR a "," (Comma) or a "b" etc.

    Did you even try this declaration in code? What did you get.

    If you tokenized your string on a, b, c, d, or e and set returnToken true
    you'd get:
    a <- one of the separators
    s <- between separators
    d <- another separator
    f <- between ...
    e <- another ...
    rgghjukluipfkjg <-- all the rest

    Does that help explain what StringTokenizer does ?

    If you really want groups of exactly 5 characters, it sounds like
    String.substring is your answer. The suggestion to use char[] sounds
    a lot like a bad attempt at premature optimization.

    -Paul
    P.Hill, Jul 28, 2004
    #10
  11. Anony!

    Cid Guest

    On Tue, 27 Jul 2004 23:13:22 GMT, "Anony!" <>
    wrote:

    >lets see i have a string
    >
    >String mystring = "asdfergghjukluipfkjg";
    >
    >i want to break it up into:
    >
    >asdfe
    >rgghj
    >uklui
    >pfkjg
    >
    >using this:
    >
    >StringTokenizer(mystring,"a,b,c,d,e",true);
    >
    >as u can see the first 5 characters dont match the pattern abcde
    >
    >Another problem is that i cant use substring(startIndex, endIndex) becuase
    >that would be hardcoding it. I want to be able to pass the string through a
    >command-line argument and break the string into substrings of 5 characters
    >in length. I didn't think it would be so damn difficult.


    Lost the first part of this thread. Did someone recommend regex to you
    yet for this? You could use a pattern that matched 5 chars at a time
    and just work that over your text.

    ---------- test.java -----------------------------------
    import java.util.regex.* ;

    public class test {
    public static void main(String args[]) throws Exception {

    String text = "asdfergghjukluipfkjg" ;
    Pattern p = Pattern.compile("[a-z]{5}") ;
    Matcher m = p.matcher(text) ;

    while ( m.find() ) {
    System.out.println( m.group(0) ) ;
    }
    }
    }

    ------------ output ----------------------------------
    asdfe
    rgghj
    uklui
    pfkjg
    Cid, Jul 28, 2004
    #11
  12. Anony!

    Roedy Green Guest

    On Tue, 27 Jul 2004 23:13:22 GMT, "Anony!" <>
    wrote or quoted :

    >Another problem is that i cant use substring(startIndex, endIndex) becuase
    >that would be hardcoding it. I want to be able to pass the string through a
    >command-line argument and break the string into substrings of 5 characters
    >in length. I didn't think it would be so damn difficult.


    If you want to break into fixed length Strings that StringTokenizer is
    the wrong tool. It breaks at certain characters.

    The tool you want is subString which will grab start, end+1.

    It is your job then to figure out how to contruct a loop that will
    execute p times where p in the number of piece you want out the end.

    Your loop needs two indexes: which chunk you are grabbing and where to
    start in the big string.

    You can compute the end position from the start position, which is a
    special case in the last chunk.

    I am not simply handing you the code just in case this is homework.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 28, 2004
    #12
  13. Anony!

    Sudsy Guest

    Anony! wrote:
    > lets see i have a string
    >
    > String mystring = "asdfergghjukluipfkjg";
    >
    > i want to break it up into:
    >
    > asdfe
    > rgghj
    > uklui
    > pfkjg
    >
    > using this:
    >
    > StringTokenizer(mystring,"a,b,c,d,e",true);
    >
    > as u can see the first 5 characters dont match the pattern abcde
    >
    > Another problem is that i cant use substring(startIndex, endIndex) becuase
    > that would be hardcoding it. I want to be able to pass the string through a
    > command-line argument and break the string into substrings of 5 characters
    > in length. I didn't think it would be so damn difficult.


    I think the reason it's "so damn difficult" is because you haven't
    conveyed PRECISELY what it is that you're trying to achieve.
    It SOUNDS like you want to split a string up into groups of 5 (even
    though you talk about not wanting to hard-code that value) and then
    compare against a fixed pattern.
    Perhaps something like this?

    String mystring = "asdfergghjukluipfkjg";
    String pattern = "abcdef";
    String temp = null;
    while( true ) {
    if( pattern.length() > mystring.length() )
    break;
    if( mystring.substring( 0, pattern.length() ).equals( pattern ) ) {
    // we have a match!
    }
    mystring = mystring.substring( pattern.length() );
    }

    Remember, I don't have ESP!... ;-)
    Sudsy, Jul 28, 2004
    #13
  14. BADBOY wrote:


    Please don't toppost.

    > wats premature optimization?
    > never heard that 1 before.


    Hint: learn to use google.

    <http://www.google.com/search?q=%22premature%20optimization%22>

    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Jul 28, 2004
    #14
  15. Anony!

    Oscar kind Guest

    Anony! <> wrote:
    > lets see i have a string
    >
    > String mystring = "asdfergghjukluipfkjg";
    >
    > i want to break it up into:
    >
    > asdfe
    > rgghj
    > uklui
    > pfkjg
    >

    [...]
    > Another problem is that i cant use substring(startIndex, endIndex) becuase
    > that would be hardcoding it. I want to be able to pass the string through a
    > command-line argument and break the string into substrings of 5 characters
    > in length. I didn't think it would be so damn difficult.


    Why would you have to hard-code it? Just use variables:

    final int PIECE_LENGTH = 5;
    int mystringLength = mystring.length();

    for (int offset=0; offset < mystringLength; offset += PIECE_LENGTH)
    {
    int endOffset = offset+PIECE_LENGTH;
    if (endOffset > mystringLength)
    {
    endOffset = mystringLength;
    }
    String subString = mystring.substring(offset, endOffset);
    System.out.println(subString);
    }



    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Jul 28, 2004
    #15
  16. Anony!

    BADBOY Guest

    wats premature optimization?
    never heard that 1 before.

    "P.Hill" <> wrote in message
    news:ce6oka$sfh$...
    > Anony! wrote:
    >
    > > lets see i have a string
    > >
    > > String mystring = "asdfergghjukluipfkjg";
    > >
    > > i want to break it up into:
    > >
    > > asdfe
    > > rgghj
    > > uklui
    > > pfkjg
    > >
    > > using this:
    > >
    > > StringTokenizer(mystring,"a,b,c,d,e",true);

    >
    > Sorry, but you need to restate your problem in a way that makes sense.
    > Your declaration of StringTokenizer will break any String whenever
    > it finds a "a" OR a "," (Comma) or a "b" etc.
    >
    > Did you even try this declaration in code? What did you get.
    >
    > If you tokenized your string on a, b, c, d, or e and set returnToken true
    > you'd get:
    > a <- one of the separators
    > s <- between separators
    > d <- another separator
    > f <- between ...
    > e <- another ...
    > rgghjukluipfkjg <-- all the rest
    >
    > Does that help explain what StringTokenizer does ?
    >
    > If you really want groups of exactly 5 characters, it sounds like
    > String.substring is your answer. The suggestion to use char[] sounds
    > a lot like a bad attempt at premature optimization.
    >
    > -Paul
    >
    >



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.727 / Virus Database: 482 - Release Date: 26/07/2004
    BADBOY, Jul 28, 2004
    #16
  17. Anony!

    Roedy Green Guest

    On Tue, 27 Jul 2004 16:31:25 -0700, "P.Hill"
    <> wrote or quoted :

    >premature optimization.


    This refers to a widely quoted statement from Donald Knuth
    see http://mindprod.com/jgloss/knuth.html

    "Premature optimisation is the root of all evil."

    The problem is fussing over fine detail to optimise wastes time and
    obscures the algorithm. Optimisation should be confined to choosing a
    good algorithm on the first pass. Only after the code is working
    should you fret over optimisation.

    Then, only if it needs it, determine the bottlenecks by measurement
    and fix only those.

    Don't do optimisations the compiler will do for you. That just
    clutters your code.

    People often misapply the nostrum to imply that optimisation itself is
    evil. Only premature optimisation is.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 28, 2004
    #17
  18. Anony!

    Roedy Green Guest

    On Wed, 28 Jul 2004 15:44:07 GMT, Christophe Vanfleteren
    <> wrote or quoted :

    >Hint: learn to use google.


    Here is a story, originally told to me by Jim Kennedy, the head of the
    UBC Department of Computer Science.

    There was a man who had a sign made for his fish store. It said

    "Fresh fish for sale".

    Then he thought. "It is obvious the how fresh the fish are. You can
    smell them." So he had a new sign made.

    "Fish for sale".

    Then he thought. "What else would I be doing with the fish but
    selling them?" So he had a new sign made.

    "Fish".

    But that too was obvious with all the fish lying about on ice. So he
    took down the sign.

    This reminds me of the way people like to rag everyone who ever asks a
    question chastising them for not finding the answer first on Google.

    They seem to expect some sort of self flagellation before every
    question to explain all the combinations they tried on Google before
    "interrupting" his holiness with a question.

    If you don't like asking and answering questions, newsgroups are not
    an appropriate place for you to hang out. Sometimes people like to
    ask questions to stimulate discussion. That does not happen in a
    private Google search.



    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 28, 2004
    #18
  19. On Wed, 28 Jul 2004 17:28:34 GMT, Roedy Green wrote:

    > They seem to expect some sort of self flagellation before every
    > question to explain all the combinations they tried on Google before
    > "interrupting" his holiness with a question.


    Try 'some sort of indication of the search terms on
    Google that failed for them' and I would say, 100%,
    yes. Or rather, no the OP soed *not* haave to supply
    that ..or anything, for that matter.

    If a problem can be solved with "try changing
    you Googling to 'setsize+bounds'.." it can save
    everybody time and effort.

    [ But, no, leave out the self-flagellation, thanks.. ]

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jul 28, 2004
    #19
  20. Anony!

    KC Wong Guest

    OT: Googling (Was Re: StringTokenizer)

    > This reminds me of the way people like to rag everyone who ever asks a
    > question chastising them for not finding the answer first on Google.
    >
    > They seem to expect some sort of self flagellation before every
    > question to explain all the combinations they tried on Google before
    > "interrupting" his holiness with a question.
    >
    > If you don't like asking and answering questions, newsgroups are not
    > an appropriate place for you to hang out. Sometimes people like to
    > ask questions to stimulate discussion. That does not happen in a
    > private Google search.


    But Christophe did provide a link to Google with proper search terms. The
    search results are very relevant and answered the OP's question nicely.

    It also tells the OP that the answers are already out there... within the
    reach of the OP's finger tips. Just Google it and the OP will find his/her
    answers much faster than waiting for replies to propagate to his/her news
    server. The OP is also likely to learn much more than to have the answers
    spoon-fed to him (there could be more sources and information available than
    in replies).

    Christophe was just introducing a more efficient way to use the web to the
    OP. I see nothing wrong with that.
    KC Wong, Jul 29, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Hanna

    Re: StringTokenizer Help

    Phil Hanna, Jul 1, 2003, in forum: Java
    Replies:
    0
    Views:
    862
    Phil Hanna
    Jul 1, 2003
  2. ARMANDO  PADILLA

    StringTokenizer help

    ARMANDO PADILLA, Jul 4, 2003, in forum: Java
    Replies:
    3
    Views:
    603
    Chris Smith
    Jul 4, 2003
  3. Liang Yew
    Replies:
    3
    Views:
    634
    Jacob
    Jul 21, 2003
  4. Jim Crowell

    StringTokenizer Question?

    Jim Crowell, Aug 20, 2003, in forum: Java
    Replies:
    9
    Views:
    3,745
    Roedy Green
    Aug 23, 2003
  5. Newbie
    Replies:
    4
    Views:
    1,443
    Stefan Siegl
    Dec 7, 2003
Loading...

Share This Page