/* * print substrings * @author Biagioni, Edoardo * @assignment lecture 2 * @date January 16, 2008 * @bugs none */ import java.util.Scanner; import java.util.InputMismatchException; public class Substring { /* this main method is invoked when the program is started * * @param arguments string representations of two integers and a string * */ public static void main(String [] arguments) { if (arguments.length != 3) { System.out.println("expecting 3 command-line arguments, got " + arguments.length); System.exit(1); } // exactly three arguments Scanner arg1 = new Scanner(arguments[0]); Scanner arg2 = new Scanner(arguments[1]); int start = 0; int finish = 0; try { // get the start position start = arg1.nextInt(); } catch(InputMismatchException ex) { System.out.println("first argument is not a number"); System.exit(1); } try { // get the finish position (plus one) finish = arg2.nextInt(); } catch(InputMismatchException ex) { System.out.println("second argument is not a number"); System.exit(1); } try { // get the substring String sub = arguments[2].substring(start, finish); // and print it System.out.println("\"" + arguments[2] + "\"" + ".substring(" + start + ", " + finish + ") = " + "\"" + sub + "\""); } catch (StringIndexOutOfBoundsException ex) { // or report that it can't be done System.out.println("illegal call to method substring"); ex.printStackTrace(); // boring stack trace... System.exit(1); } } }