Computer Science 225
Advanced Programming
Spring 2017, Siena College
FindInterstates BlueJ Project
Click here to download a BlueJ project for FindInterstates.
FindInterstates Source Code
The Java source code for FindInterstates is below. Click on a file name to download it.
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
* Example FindInterstates: look for interstate highway references
* in a METAL tmg graph file's vertex labels, and report how many are found.
*
* @author Jim Teresco, Siena College, Computer Science 225, Spring 2017
*
*/
public class FindInterstates {
/**
* main method to read in a METAL .tmg file as specified in args[0]
* and print a count of how many vertices in the graph appear to have
* a reference to an interstate highway
*
* @param args command line parameters, where args[0] is expected
* to have a filename for a METAL tmg graph file
*/
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java FindInterstates tmgfile");
System.exit(1);
}
// create a Pattern that will match anything with I- followed by 1 or
// more numbers, with special cases of A (for Alaska interstates), H (for
// Hawaii interstates), or PR (for Puerto Rico interstates)
Pattern p = Pattern.compile("I-(A|H|PR)?\\d+");
int iCount = 0;
int numV = 0;
try (Scanner s = new Scanner(new File(args[0]))) {
// first line is version and format info, which we assume is good
s.nextLine();
// second line is number of vertices and number of edges in file
numV = s.nextInt();
s.nextInt();
for (int v = 0; v < numV; v++) {
// create a matcher over the vertex label
Matcher m = p.matcher(s.next());
if (m.find()) iCount++;
// skip over the lat/lng info that we don't care about here
s.nextLine();
}
s.close();
}
catch (IOException e) {
System.err.println(e);
System.exit(1);
}
System.out.println("Found " + iCount + " interstate references among " + numV +
" vertex labels in " + args[0]);
}
}