Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data structure help

 
Sam Battery
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sam Battery
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic