Help on StringTokenizer
I am developing a program to read a logfile and display the selected data in different windows.
In one window I have to add a series of buttons (A..Z) and when I click a button it should display data starting with that letter. for e.g.
0.01 0.01 191650 5 | ae United Arab Emirates
0.01 0.01 146570 8 | am Armenia
0.04 0.04 994907 36 | ar Argentina
0.23 0.21 5282499 212 | at Austria
1.89 2.11 51905657 1729 | au Australia
0.60 0.54 13423956 545 | be Belgium
0.01 0.01 316190 6 | bh Bahrain
0.00 0.00 7397 2 | bn Brunei Darussalam
When I click button A, it should display the data for Armenia, Argentina so on.
I managed to add a series of buttons and attach action listeners to them but I cannot figure out a method to go through the logfile and compare the country names starting for the relevant button.
Could anyone suggest something for this please? (I use the stringtokenizer to tokenize the string)
Many thanks in advance!
Here's a suggestion.
Do this once at the start...
Walk the log file, and use a StringTokenizer (or something similar) to parse each line. Create an object (from a class like "CountryData" for example) for each one, encapsulating the info on the line, making it accessible through methods like getName() getAbbreviation(), etc.
Add these objects to a growing List.
Now, you need to know which position on the list the 'a's start at, where the 'b's are etc. So as you're building the list, keep track of the position of the first object matching 'a' in an array of ints, with one slot for each letter.
When you have finished reading the file, it will all be in memory, in a big list, and you know where each block starts. Keep the array and list around in memory; there's obviously no point in reading it every time someone presses a key.
So when someone presses 'P', go to your array, figuring that 'P' is the 16th letter, and look up the position in the big list of the first item corresponding to 'P'. Now go to your big list and start from this position picking the items on the list and displaying whatever info you want.
An alternative approach would be to store lists of all the CountryData objects corresponding to 'A' in one list, all the ones for 'B' in another etc. Pop each list onto a HashMap keyed by the letters.
The approach should be to read the data from the file and store it in memory , organised in a way that suits your application.
Hope these ideas help.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center