• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

StringTokenizer length

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,but am using java 4

Thanks
[ July 29, 2007: Message edited by: chen yanush ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic