• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringTokenizer length  RSS feed

 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
chen young
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,but am using java 4

Thanks
[ July 29, 2007: Message edited by: chen yanush ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!