/** * This class contains methods to extract parts of an input line of the * form (Station1, Station2, line). The strings Station1, Station2 and line * may contain any characters (including spaces) except commas and right parens. * The strings returned will have all leading and trailing spaces trimmed. *

* For use in Assignment 6 * * @author Margaret Lamb */ public class GraphInput { /** * A type of exception to raise if there is an error in the input format. * Because it is a subclass of RuntimeException, the user does not have to * use a try/catch around methods that throw this exception. */ public static class GraphInputFormatError extends RuntimeException { /** * Creates a new GraphInputFormatError exception with a message identifying * the problem * * @param msg a message identifying the format error (e.g. input line * does not start with a left paren) */ public GraphInputFormatError(String msg) { super(msg); } // end constructor } // end class GraphInputFormatError /** * Extracts the first station name from an input line of the form * "(Station1, Station2, line)". The station name will have all leading * and trailing spaces trimmed. For example, if the input line is
* " ( Baker Street , Royal Albert, Victoria Line ) ", this * method will return "Baker Street". * * @param inputLine the line of input * @return the first station name from the input line * @exception GraphInputFormatError if the input line does not have the * correct format (e.g. doesn't start with a left paren, or doesn't have * two commas) */ public static String getStation1(String inputLine) { inputLine = inputLine.trim(); // remove any leading spaces if (inputLine.length() == 0 || inputLine.charAt(0) != '(') { // doesn't start with a left paren throw new GraphInputFormatError( "input line does not start with left paren"); } // end if int commaPos = inputLine.indexOf(','); // position of first comma if (commaPos == -1) { // no commas in input line throw new GraphInputFormatError( "input line does not contain a comma"); } // end if // Station1 is everything after the left paren and before the comma, // minus any leading or trailing spaces return inputLine.substring(1, commaPos).trim(); } // end getStation1 /** * Extracts the second station name from an input line of the form * "(Station1, Station2, line)". The station name will have all leading * and trailing spaces trimmed. For example, if the input line is
* " ( Baker Street , Royal Albert, Victoria Line ) ", this * method will return "Royal Albert". * * @param inputLine the line of input * @return the second station name from the input line * @exception GraphInputFormatError if the input line does not have the * correct format (e.g. doesn't start with a left paren, or doesn't have * two commas) */ public static String getStation2(String inputLine) { int commaPos1 = inputLine.indexOf(','); // position of first comma if (commaPos1 == -1) { // no commas in input line throw new GraphInputFormatError( "input line does not contain a comma"); } // end if int commaPos2 = inputLine.indexOf(',', commaPos1+1); if (commaPos2 == -1) { // only one comma in input line throw new GraphInputFormatError( "input line contains only one comma"); } // end if // Station1 is everything between the two commas, // minus any leading or trailing spaces return inputLine.substring(commaPos1+1, commaPos2).trim(); } // end getStation2 /** * Extracts the line name from an input line of the form * "(Station1, Station2, line)". The line name will have all leading * and trailing spaces trimmed. For example, if the input line is
* " ( Baker Street , Royal Albert, Victoria Line ) ", this * method will return "Victoria Line". * * @param inputLine the line of input * @return the line name from the input line * @exception GraphInputFormatError if the input line does not have the * correct format (e.g. doesn't start with a left paren, or doesn't have * two commas) */ public static String getLine(String inputLine) { // trim any trailing blanks so the ')' is at the end inputLine = inputLine.trim(); int commaPos1 = inputLine.indexOf(','); // position of first comma if (commaPos1 == -1) { // no commas in input line throw new GraphInputFormatError( "input line does not contain a comma"); } // end if int commaPos2 = inputLine.indexOf(',', commaPos1+1); // position of 2nd comma if (commaPos2 == -1) { // only one comma in input line throw new GraphInputFormatError( "input line contains only one comma"); } // end if int parenPos = inputLine.length() - 1; if (inputLine.charAt(parenPos) != ')') { // input line doesn't end with right paren throw new GraphInputFormatError( "input line does not end with a right paren"); } // end if // Station1 is everything between the second comma and the right paren // minus any leading or trailing spaces return inputLine.substring(commaPos2+1, parenPos).trim(); } // end getLine /** * method to test this class */ public static void test () { String samples[] = { " ( Baker Street , Royal Albert, Victoria Line ) ", "(long station with spaces,longstationwithnospaces,line)", "(Acton_Town, Ealing_Common, Distric)", "(Acton_Town, South_Ealing, Piccadilly_Line)" }; for (int i = 0; i < samples.length; i++) { System.out.println("Input Line: \"" + samples[i] + "\""); // Vertex v = new Vertex(getStation1(samples[i])); System.out.println(" Station1 = \"" + getStation1(samples[i]) + "\""); System.out.println(" Station2 = \"" + getStation2(samples[i]) + "\""); System.out.println(" line = \"" + getLine(samples[i]) + "\""); } // end for } // end test } // end class GraphInput