• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizer help

 
Imran Bhutta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...



thankx
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34210
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imran,
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.
 
Imran Bhutta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne,
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Layne
 
Imran Bhutta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic