aspose file tools*
The moose likes Beginning Java and the fly likes String Tokenizer help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Tokenizer help" Watch "String Tokenizer help" New topic

String Tokenizer help

Imran Bhutta

Joined: Oct 31, 2004
Posts: 9
i have a program that is using string tokenizer to grab a char and a double value, but the file's data is stored like this
c 500
c 21000
p 550
c 5950
c 3000
l 4000
p 4050
and so on........... where i have that arrow is where i need help how would i get string tokenizer to skip that line and IO.prinln("invalid entry") but then continue on grabing the next values such as l 4000 and p 4050... so far my code goes and prints out everthing up to c 3000 and then crashes...

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31062

StringTokenizer can't pick that up. But you can skip that line completely. For example,

Trim() gets rid of any whitespace in the beginning and end of the string. This will skip blank lines.

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Imran Bhutta

Joined: Oct 31, 2004
Posts: 9
but i need that blank space to be accounted for, when it hits that blank line it need to print out invalid record.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
You can easily modify Jeanne's suggestion to do what you want:

As you work on building your programming tool box, things like this should eventually become second-nature.

Good luck and Keep Coding!


Java API Documentation
The Java Tutorial
Imran Bhutta

Joined: Oct 31, 2004
Posts: 9
ohh lol yea durrr thankx, now i have another question, if u want to make like a chart, like

0000 c 500.00
0001 c 2100.00

you would use \t right?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Imran Bhutta:
if u want to make like a chart ... you would use \t right?
Yes and no. It depends on the output device (terminal I assume here), but typically \t stops are set every eight columns. There's no guarantee, however. As well, what happens if one column will contain four to ten characters. That means sometimes the next column won't be straight from line to line.

If you're not too worried about that, yes \t will do what you want. If you need to be really anal about the table, you can make your own columns by outputing enough spaces to fill the column out. Here's a simple example:
[ November 07, 2004: Message edited by: David Harkness ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd probably pad each column of the table with spaces. A quick & dirty way to put leading zeros or spaces...

column = "00000000".substring(value.length()) + value;

A more elegant way is to make a reusable utility somewhere:

column = Util.right(value, 8, "0");

That would mean right justify to a length of 8 padded with zeros. I have right, left and center methods I use to produce reports from arbitrary SQL queries. You can get clever ... to underline column headings:

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
I agree. Here's the link:
subject: String Tokenizer help