StringTokenizer

A

Anony!

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
 
J

jAnO!

Anony! said:
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
 
A

Anony!

VisionSet said:
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
 
V

VisionSet

Anony! said:
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.
 
A

Alan Moore

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().
 
B

BADBOY

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...
 
O

Oscar kind

Anony! said:

[Discussion on java.util.StringTokenizer]
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.
 
A

Anony!

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 said:
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! said:
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
 
P

P.Hill

Anony! said:
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
 
C

Cid

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
 
R

Roedy Green

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.
 
S

Sudsy

Anony! said:
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!... ;-)
 
O

Oscar kind

Anony! said:
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);
}
 
B

BADBOY

wats premature optimization?
never heard that 1 before.

P.Hill said:
Anony! said:
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
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
A

Andrew Thompson

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.. ]
 
K

KC Wong

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top