my dog learned polymorphism*
The moose likes Java in General and the fly likes Data structure help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Data structure help" Watch "Data structure help" New topic
Author

Data structure help

Sam Battery
Greenhorn

Joined: Oct 05, 2000
Posts: 10
I'm working on an applet that draws information from a text file for display in text fields for calculation. The trouble I'm having is this:
The numbers are broken down by state.
Then each state has 3 columns of figures (all of the same data type) broken down by type. There are 4 rows in each column.
I don't know how to draw the data from the file into the applet and how to store it once it has been added. I figure I would use a stream to input the data but how do I tell the stream which state to draw from and which column within the state to display. What sort of data structure (array, Vector, hashtable) would I use and how do I write my file objects to it? I'm new to Java and this is the most complicated thing I've had to do. Any help would be most appreciated. Thank you in advance.
------------------
Marius Holm
Ranch Hand

Joined: Sep 11, 2000
Posts: 84
Jeff,
I think you should provide more info so that people understand your problem better. It could be my english, but I didn't get your point with states and what the numbers were for. (United States or program states? )
Regards,
Marius
[This message has been edited by Marius Holm (edited January 22, 2001).]
Sam Battery
Greenhorn

Joined: Oct 05, 2000
Posts: 10
I'm sure your English is fine. I was referring to U.S. states. The data I'm working with is the alcohol tax rate for individual U.S. states. The user will select his state from a Choice list, then three columns of TextFields will populate with the tax data for liquor, beer, and wine. Consumption rates for each type of alcohol will also be included. The data can be organized in a text file any way I see fit, but I don't know how to go about retrieving the right data and storing it for access by the applet.
------------------
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Jeff,
Here is the way I would do it... I would have the text file on the server (www.someserver.org/~jeff/liquor.txt) and either hardcode this into the applet or have it passed in as a parameter. (i.e. somehow it basically has to boil down to

Then, use this URL to open a InputStream :

Then create a StreamTokenizer based off the InputStream :

Hope you could understand the code above... Basically, a StreamTokenizer doesn't take an InputStream directly, but it will take a BufferedReader, which takes a InputStreamReader, which takes an InputStream (finally! ).
Then just process your records... you will need to look at documentation on StreamTokenizer ( http://java.sun.com/j2se/1.3/docs/api/java/io/StreamTokenizer.html ) and look at your data to see how to do this, and you can store this data in your program any way you wish (i.e. arrays, Collections, or new class...). Basically to read a token from input you call st.nextToken(), and assign it to variables with st.sval for Strings, st.nval for doubles (which can then be cast to other types).
HTH,
-Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Sam Battery
Greenhorn

Joined: Oct 05, 2000
Posts: 10
Thanks I've never heard of that. How would I do it with a file object instead of a URL? What sort of exceptions does this thing throw?
------------------
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
I've an Applet live online with the source code, which used the technique Nathan Pruett described, and it can used as an application too. You can see the comparison of them.
Take a look:
http://www.geocities.com/roseannezhang/Flag/FlagApplet.html
Be a little patient to wait for some image downloads.
Thanks!
Roseanne
 
Don't get me started about those stupid light bulbs.
 
subject: Data structure help