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
posted
0
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
posted
0
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