• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringTokenizer problem!

 
Mike Mass
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am facing a dilemma regarding my StringTokenizer.
I have this line as input :

treasureisland adventure The movie is based blabla. More blabla the end.

I declare a default stringtokenizer for this string.(delimiter = spaces)

By reading the first 2 tokens, i read the moviename and the movie type.
Is there a way to grab whats left of the string into movieDescr without having to read tokens while hasmoretokens? (since that might alter the formatting of the text, ie losing any spaces in the text)

this is my only solution so far, any other ideas?

while(resToken.hasMoreTokens())
{
resValue +=" " + resToken.nextToken();
}

Thanks in advance!
Mike Mass
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try split()
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Mass:
Hi all,

I am facing a dilemma regarding my StringTokenizer.
I have this line as input :

treasureisland adventure The movie is based blabla. More blabla the end.

I declare a default stringtokenizer for this string.(delimiter = spaces)

By reading the first 2 tokens, i read the moviename and the movie type.
Is there a way to grab whats left of the string into movieDescr without having to read tokens while hasmoretokens? (since that might alter the formatting of the text, ie losing any spaces in the text)

this is my only solution so far, any other ideas?

while(resToken.hasMoreTokens())
{
resValue +=" " + resToken.nextToken();
}

Thanks in advance!
Mike Mass



The 2 is to handle the space between name and type and the space between type and remainder of line (which I presume you don't want at the start of the description.
 
Mike Mass
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,


thanks for your input regarding my question, The solution you propose would fail if the data is entered by a human being, as in extra spaces and tabs.

Also, thank you Joanne for your input, but we would fall back in the problem i mentioned above to Michael, if the user enters extra spaces or a tab, it wouldnt work.

Thanks alot for your answers guys,
Mike Mass
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> The solution you propose would fail if the data is entered by a human being,

then you handle it.

get the line, replaceAll() to convert whitespace+ to a single space.
 
Ryan McGuire
Ranch Hand
Posts: 1068
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


...assuming that '|' isn't a valid character in the description.

Or just use Michael Dunn's initial code where he uses split() but make the firs arg "\s*".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic