Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > StringTokenizer

Reply
Thread Tools

StringTokenizer

 
 
Cid
Guest
Posts: n/a
 
      07-27-2004
On Tue, 27 Jul 2004 23:13:22 GMT, "Anony!" <(E-Mail Removed)>
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

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-28-2004
On Tue, 27 Jul 2004 23:13:22 GMT, "Anony!" <(E-Mail Removed)>
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.
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      07-28-2004
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!...

 
Reply With Quote
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      07-28-2004
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
 
Reply With Quote
 
Oscar kind
Guest
Posts: n/a
 
      07-28-2004
Anony! <(E-Mail Removed)> 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
 
Reply With Quote
 
BADBOY
Guest
Posts: n/a
 
      07-28-2004
wats premature optimization?
never heard that 1 before.

"P.Hill" <(E-Mail Removed)> wrote in message
news:ce6oka$sfh$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-28-2004
On Tue, 27 Jul 2004 16:31:25 -0700, "P.Hill"
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-28-2004
On Wed, 28 Jul 2004 15:44:07 GMT, Christophe Vanfleteren
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      07-28-2004
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
 
Reply With Quote
 
KC Wong
Guest
Posts: n/a
 
      07-29-2004
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
StringTokenizer insert into Array?? Newbie Java 4 12-07-2003 06:19 PM
StringTokenizer Question? Jim Crowell Java 9 08-23-2003 06:28 PM
Read data with delimater using stringtokenizer Liang Yew Java 3 07-21-2003 07:10 AM
StringTokenizer help ARMANDO PADILLA Java 3 07-04-2003 04:37 PM
Re: StringTokenizer Help Phil Hanna Java 0 06-30-2003 11:57 PM



Advertisments