• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unexpected results from StringTokenizer  RSS feed

 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input file:
110001 commercial 500000.00 101
110223 residential 100000.00 101
110020 commercial 1000000.00 107
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110


Already declared arrays for each column.

Then program is to display the collected array info on screen formatted appropriately.
But the output on the console is:
110001 commercial 500000.0 101
110020 commercial 1000000.0 107
110442 farm 200000.0 106
112352 residential 250000.0 110
0 null 0.0 0

I'm wondering where the other tokens are going... is the program using \n as a token? It seemed to get only every other line, with a null line at the end.

Thought it was pretty straightforward: each column's token goes into an array as appropriate type (int, double, or string), then advance the array one number and repeat.

 
Rob Spoor
Sheriff
Posts: 20902
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't do it like this. You put your file into memory completely, but you only need one line at a time. Why not read the file line by line, then parse that line:
On a side note, about your use of StringBuffer:
1) use StringBuilder instead. StringBuffer adds unnecessary synchronization
2) StringBuffer has method append(char[] str, int offset, int len). That means you can call append(buffer, 0, charactersIn), and you don't need to create a new String just for adding it.
 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a valid idea, and is the method I would prefer to use. However, I'm not permitted to use a BufferedReader. It must be unbuffered when being read into the program.
Therefore, I made one big string and then tokenized it. Why though, is it reading basically half the tokens and leaving out the rest? How can I get it to get them from each line and sorted into the appropriate arrays, as it currently does with only every other line of the initial file?
 
Rob Spoor
Sheriff
Posts: 20902
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why must it be unbuffered? You are now essentially creating the biggest buffer there is - the file itself is completely buffered into your StringBuffer.
 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know and agree. This is the first of a two-part exercise to show me the difference between unbuffered and buffered FileReaders. The teacher requires this program to be unbuffered. I have a similar exercise next that will have BufferedReader.
I can buffer it AFTER FileReader has done its thing, and so I did to make it easier. I think.
 
Rob Spoor
Sheriff
Posts: 20902
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us your output code? I've just copied your code, added my arrays, then printed it out. I got my input back. After adding formatting to the doubles the file was even identical.
 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Output on console screen is as follows:

Enter the name of your listing file:
listings.txt
110001 COMMERCIAL 500000.0 101
110020 COMMERCIAL 1000000.0 107
110442 FARM 200000.0 106
112352 RESIDENTIAL 250000.0 110
0 null 0.0 0



Slightly modified code while I was testing different things:


Then had a few System.out.println lines to show what had been read in, as I pasted at first.

Initial listings file:
110001 commercial 500000.00 101
110223 residential 100000.00 101
110020 commercial 1000000.00 107
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110

 
Rob Spoor
Sheriff
Posts: 20902
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original reading code worked for me. So my question was, and still is, how do you verify the contents? How do you produce that output?
 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holy crap I pulled another dumb mistake. Your asking me to post the "displayer" code made me see it.
I was using a "for loop" to go through the arrays to display them on-screen.
However, I also advanced the array line by one INSIDE the loop. Therefore it would show every OTHER line. Which it did.

I am really really sorry for wasting your time with a stupid mistake.
Also, I am very grateful for your help with this. I've been messing with this program for FAR too long, and I guess I just needed another set of eyes to clue me in to my noobie errors.
Thank you!!!
 
Rob Spoor
Sheriff
Posts: 20902
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Spurgeon wrote:I am really really sorry for wasting your time with a stupid mistake.

It's OK. You thought the problem was in one piece of code, and it took me all-in-all less then 10 minutes (including all replies) to conclude it wasn't located there. So not much time was spent, and since you've found the answer it wasn't wasted at all.

Also, I am very grateful for your help with this. I've been messing with this program for FAR too long, and I guess I just needed another set of eyes to clue me in to my noobie errors.

It's quite normal to not see the most simplest of solutions, and I too require a fresh set of eyes sometimes.

Thank you!!!

You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!