aspose file tools
The moose likes Java in General and the fly likes StringTokenizer length Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "StringTokenizer length" Watch "StringTokenizer length" New topic
Author

StringTokenizer length

chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 177
Hi

I am writing data into .dat file.
I set a const column length (for example 20 char).

For each token I need to check the length and to add the remainder length so the next token will be written after 20 char and the next one will be written 40 char etc.

StringTokenizer stringTokenizer = new StringTokenizer("this is a test");
while (stringTokenizer.hasMoreTokens()) {
System.out.print(stringTokenizer.nextToken());

Any advice

Thanks
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
I guess you want to know the length of each token. You can use string.length():

String token = stringTokenizer.nextToken();
int length = token.length();

Hope this helps
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Sounds like you want to pad each token to 20 characters to make nice looking columns in the output?

If that's the question, up to release 6 you had to add the spaces yourself. With Java 6 you can use the width option with printf() on PrintWriter, PrintStream and Console classes.

Is that the right topic?
[ July 29, 2007: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
chen young
Ranch Hand

Joined: Sep 09, 2005
Posts: 177
Yes,but am using java 4

Thanks
[ July 29, 2007: Message edited by: chen yanush ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer length
 
Similar Threads
doPost method
Some String-like questions
Runaway Code
How to form a new string out of several old strings excluding some??.......
String Tokenizer