parsing a string using Stringtokenzier

V

vic

Hi All,

I have a string x=RECORD(1) state:xx city:yyyyyy zip:12345
how would i go about so that i can derive the values for state, city
and zip and store it in a new string . i did try and use
stringtokenizer but could get it to work. Am i missing something.

here is what i tried

String tstate = " ";
String tcity = " ";
String tzip = " ";

StringTokenizer str = new StringTokenizer(x, ":");
while(str.hasMoreTokens()) {
tstate = str.nextToken(":");
tcity = str.nextToken(":");
tzip = str.nextToken(":");
}

Any help appreciated

Thanks

vic
 
A

Andrew Hobbs

vic said:
Hi All,

I have a string x=RECORD(1) state:xx city:yyyyyy zip:12345
how would i go about so that i can derive the values for state, city
and zip and store it in a new string . i did try and use
stringtokenizer but could get it to work. Am i missing something.

Yes. You either haven't read the docs for StringTokenizer or you didn't pay
attention when you skimmed through it. (I presume you meant that you 'could
*not* get it to work.')
here is what i tried

String tstate = " ";
String tcity = " ";
String tzip = " ";

StringTokenizer str = new StringTokenizer(x, ":");
while(str.hasMoreTokens()) {
tstate = str.nextToken(":");
tcity = str.nextToken(":");
tzip = str.nextToken(":");
}

Any help appreciated

It would help if you indicated in what way it wouldn't work. I presume you
got something like

tstate = "RECORD(1) state"
tcity = "xx city"
tzip = "yyyyyy zip"

Anyway. If you have a look at the java docs for StringTokenizer you will
see that it parses the string into tokens which begin and end at one or
other of the delimiters you have given. Since you have only given ":" then
that is it. So look carefully at your string and see what it is likely to
come up with. In this case what you need to do is provide the delimiters as
" :" (ie include a blank space) and then discard tokens you do not want.
(Actually it is probably better to take a token and if it corresponds to the
name of a desired field then use the next token for that field. Then if the
order of the fields is changed at any time then it should still work.)

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

vic said:
Hi All,

I have a string x=RECORD(1) state:xx city:yyyyyy zip:12345
how would i go about so that i can derive the values for state, city
and zip and store it in a new string . i did try and use
stringtokenizer but could get it to work. Am i missing something.

IMO, StringTokenizer shouldn't be used for anything more complicated
than splitting simple strings with simple delimiters. Try investigating
the java.util.regex library, or roll your own parser.
 
V

vic

Hi All,

I have a string x=RECORD(1) state:xx city:yyyyyy zip:12345
how would i go about so that i can derive the values for state, city
and zip and store it in a new string . i did try and use
stringtokenizer but could get it to work. Am i missing something.

here is what i tried

String tstate = " ";
String tcity = " ";
String tzip = " ";

StringTokenizer str = new StringTokenizer(x, ":");
while(str.hasMoreTokens()) {
tstate = str.nextToken(":");
tcity = str.nextToken(":");
tzip = str.nextToken(":");
}

Any help appreciated

Thanks

vic

i was looking for an output like this

tstate = xx
tcity = yyyyyy
tzip = 12345

thanks
 
A

Andrew Thompson

(e-mail address removed) (vic) wrote in message news:<[email protected]>... .....

Does it always start with
x=REDORD(1), is that part of
the string?

Initializing the string with a
space makes no sense. Set it to
"" if you require it to be non-null.

delimiter ":" is a good start,
but make it ": "

You have already set the delimiter
to ":", no need to set it here

...or here, etc.

but since you are enumerating the
tokens, calling it three times makes
no sense either

You failed to mention what
happened next, what was you
output?

You should be System.out.println()
everything that is happening.
....
i was looking for an output like this

tstate = xx
tcity = yyyyyy
tzip = 12345

The source of a working StringTokenizer
example is available from the first
link I gave you, which leads here..
<http://www.physci.org/launcher.jsp?class=/codes/StringTokenizer/StringTokenizerFrame>

On this page is a button that
launches the application, click
the button. Cut and paste your
string on the top line, then
type ": " on the next line and
tokenize it, look at the output
in the bottom window, that
might give you an idea how to
proceed.

At the moment I think you might
be better off posting to
comp.lang.java.help, it is
more suited to people just starting
Java.

HTH
 
D

Dale King

Daniel Sjöblom said:
vic wrote:

IMO, StringTokenizer shouldn't be used for anything more complicated
than splitting simple strings with simple delimiters

Sorry, I only agree with the first part of that sentence.

IMO, StringTokenizer shouldn't be used for anything.

;-)

It has confused people, can't do common operations that people want, and its
behavior is not consistent from one version of the JDK to the next.
Basically, it should be deprecated at this point.
 
V

vic

Andrew Thompson said:
Does it always start with
x=REDORD(1), is that part of
the string?


Initializing the string with a
space makes no sense. Set it to
"" if you require it to be non-null.


delimiter ":" is a good start,
but make it ": "


You have already set the delimiter
to ":", no need to set it here


..or here, etc.


but since you are enumerating the
tokens, calling it three times makes
no sense either


You failed to mention what
happened next, what was you
output?

You should be System.out.println()
everything that is happening.
...


The source of a working StringTokenizer
example is available from the first
link I gave you, which leads here..
<http://www.physci.org/launcher.jsp?class=/codes/StringTokenizer/StringTokenizerFrame>

On this page is a button that
launches the application, click
the button. Cut and paste your
string on the top line, then
type ": " on the next line and
tokenize it, look at the output
in the bottom window, that
might give you an idea how to
proceed.

At the moment I think you might
be better off posting to
comp.lang.java.help, it is
more suited to people just starting
Java.

HTH

Thanks Andrew, appreciate your help.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top