File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer problem! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer problem!" Watch "StringTokenizer problem!" New topic
Author

StringTokenizer problem!

Mike Mass
Greenhorn

Joined: Aug 13, 2004
Posts: 12
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

Joined: Jun 09, 2003
Posts: 4632
try split()
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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.


Joanne
Mike Mass
Greenhorn

Joined: Aug 13, 2004
Posts: 12
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Feb 18, 2005
Posts: 1013
    
    3


...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*".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer problem!