Forums Register Login

Unexpected results from StringTokenizer

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Why must it be unbuffered? You are now essentially creating the biggest buffer there is - the file itself is completely buffered into your StringBuffer.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
 

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.
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1293 times.
Similar Threads
Memory leak while using String tokenizer
jTextArea - problem with \u
Map Key Add up issue
FileIO and mult. arrays
Java NullpointerException while reading BufferedReader
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:56:31.