Reading two (or more) variables from one file...

Got a message from one of the forum members:

"i read your posting on screen scraper and was wondering if you knew how to read to variables from one file?

I need to send in a city and state on a store locator and have a list but do not know how to read in two variables? I can read one in like a zip code but not two."

I'm posting the answer here, because I'm sure there are others who will want to do something similar.

Actually, with this, you can add as many variables from one file as you like, as long as you use a delimiter to separate them. The delimiter can be whatever you like (space, comma, etc.).

Below is the code. I haven't tested it, but if it doesn't work, post and let me know. It should work, or be very close to working.

// Pause timer (5 seconds)
pauseTimer = 5000;

// Create a file object that will point to the file
// containing the search terms (in this case, city_state.txt).
File inputFile = new File( "city_state.txt" );
FileReader in = new FileReader( inputFile );
BufferedReader buffRead = new BufferedReader( in );

// Create a String array and two ArrayLists to hold the list contents.
// If you're reading more than two variables, be sure to initialize
// more than two ArrayLists.
String[] arrSplit = null;
ArrayList arrCity = new ArrayList();
ArrayList arrState = new ArrayList();

Loop1Counter = 0;

// Read the file in line-by-line
// Split the line into two parts (the comma is the split point)
// add the two parts of the line into a string array
// add the two parts of the string array to the individual ArrayLists.
while( ( searchCityState = buffRead.readLine() )!=null)
{
// If you're using something other than a comma to separate
// the values, change the comma in this line to whatever you're using.
arrSplit = searchCityState.split(",");
temp = arrSplit[0];
arrCity.add(temp);
temp = arrSplit[1];
arrState.add(temp);

// If you've got more than 2 variables to add from your source file,
// just add more
// temp=arrSplit[x];
// and
// arrWhatever.add(temp);
// lines here.

arrSplit = null;
}

// Calculate the lengths of the arrCity and arrState ArrayLists.
// Use this data however you wish.
arrCitySize = arrCity.size();
arrStateSize = arrState.size();

// Do whatever you wish to do with the arrCity and arrState ArrayLists here.
// Remember that the ArrayLists start with a count of 0, so you'd access it like this:
// arrCity.get(0);
// arrCity.get(1);
// arrCity.get(2);
// etc., etc.
// It's probably best to do this in a loop, like so:
for( Loop1Counter = 0; Loop1Counter < arrCitySize; Loop1Counter++ )
{
LatestCity = arrCity.get(Loop1Counter);
LatestState = arrState.get(Loop1Counter);

// Do whatever you want with the LatestCity and LatestState variables here.

session.pause( pauseTimer );

}

// Notice that as long as each line of the data you're reading in has a City
// and a State entry, the two will stay synchronized in the two separate
// ArrayLists (arrCity, arrState).
// If you have some that don't have one of the parts of the data, they will
// no longer be synchronized.

// Make some noise to let us know you're done.
java.awt.Toolkit.getDefaultToolkit().beep();
session.pause( pauseTimer );
java.awt.Toolkit.getDefaultToolkit().beep();
session.pause( pauseTimer );
java.awt.Toolkit.getDefaultToolkit().beep();
session.pause( pauseTimer );
java.awt.Toolkit.getDefaultToolkit().beep();
session.pause( pauseTimer );
java.awt.Toolkit.getDefaultToolkit().beep();
session.pause( pauseTimer );
java.awt.Toolkit.getDefaultToolkit().beep();

// Close up the objects to indicate we're done reading the file.
Loop1Counter = 0;
arrCity.clear();
arrState.clear();
arrSplit.clear();
in.close();
buffRead.close();