[CSC 204] Extracting data from weather data files...

Andrew J. Pounds pounds_aj at mercer.edu
Sat Nov 1 12:55:36 EDT 2014


Alright -- I am EXCITED that some of you are actually working ahead on 
the weather data program.  I am now getting questions like "how can I 
get the data once I read the file" or "can I do it this way"...

Please understand, there is no defined method for extracting the 
information.   Here is, however, a method that should work. Let's say 
that we are trying to extract the information out of the station.txt 
file.   You might have all of the information in a String variable 
called "line".   You also know that the first piece of information in 
the file is the station number and that the file is delimited by "|" 
marks.   This could work...

String stationID = line.substring(0,line.indexOf("|")).trim();


Now, to get over to the state info I need to read past several of those 
"|" delimiters and then extract the info.   The following should work...

boolean found = false;
int location = 0;

index = 0;
int numberFound=0;
while ( !found && index < line.length()  ) {
     if ( line.charAt(index) == '|' ) numberFound++;
          if ( numberFound == 7 ) {
                location = index;
                found = true;
     }
     index++;
}
String stationState = 
line.substring(location+1,line.indexOf("|",location+1));


Take a look at the string API to see what I am doing on that last line.  
To see if your code is working, print out the stationID and the state.


-- 
Andrew J. Pounds, Ph.D.  (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627
http://faculty.mercer.edu/pounds_aj

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc204/attachments/20141101/22dc7f99/attachment.html>


More information about the csc204 mailing list