import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /* Java application having as its purpose to test the methods in the ** CSV_Utility class. ** Author: R. McCloskey */ public class CSV_Utility_Tester { private static Scanner input; private static boolean echo; private static boolean keepGoing; private static String s; public static void main(String[] args) { establishInputSource(args); s = getString("Enter a CSV string: "); keepGoing = true; while (keepGoing) { try { performCommand(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("\nGoodbye."); } private static void performCommand() { String COMMAND_CODES = "QHP#GSRIN"; // Prompt for a command and read it. String command = getString("\nEnter command (H for help): ").trim(); // Parse the command. char operation = Character.toUpperCase(command.charAt(0)); if (COMMAND_CODES.indexOf(operation) == -1) { System.out.println("ERROR: Invalid command"); } else if (operation == 'Q') { keepGoing = false; } else if (operation == 'H') { displayHelp(); } else if (operation == 'P') // print the current string { System.out.println(s); } else if (operation == '#') { System.out.println(CSV_Utility.numberOfFieldsIn(s)); } else if (operation == 'N') // establish new current CSV-string { s = command.substring(2); } else { String rest = command.substring(1); // Each of remaining operations requires that an integer appears // after the command code. Scanner commandScanner = new Scanner(rest); int k = commandScanner.nextInt(); if (operation == 'G') { System.out.println(CSV_Utility.getIthField(s,k)); } else if (operation == 'R') { s = CSV_Utility.removeIthField(s,k); System.out.println(s); } else { // Each of two remaining operations requires that a field value // appears next. String field = commandScanner.nextLine().trim(); if (field.indexOf(',') != -1) { System.out.println("**ERROR; a field cannot include a comma"); } else { // The remaining possibilities are 'S' and 'I' if (operation == 'S') { s = CSV_Utility.setIthField(s,k,field); } else if (operation == 'I') { s = CSV_Utility.insertIthField(s,k,field); } else { System.out.println("This is impossible!"); } } System.out.println(s); } } } private static void displayHelp() { System.out.println("Examples of commands:"); System.out.println(" H (display this!)"); System.out.println(" Q (quit)"); System.out.println(" P (print current string)"); System.out.println(" # (ascertain # fields)"); System.out.println(" G 5 (print field #5 in current string)"); System.out.println(" R 3 (remove field #3 from current string)"); System.out.println(" S 2 glork (set field #2 to \"glork\")"); System.out.println(" I 4 glork (insert \"glork\" as field #4)"); System.out.println(" N don't,look,now (establish a new CSV-string)"); } /* Displays the given prompt and returns the response to it, as read ** using the 'input' Scanner. (If 'echo' is true, indicating that the ** input is being read from a file --as opposed to the keyboard-- the ** response is printed.) */ private static String getString(String prompt) { System.out.print(prompt); String response = input.nextLine(); if (echo) { System.out.println(response); } return response; } /* Attaches the 'input' Scanner to either the keyboard or to ** the file named by args[0], the latter if args.length != 0. ** Also assigns value to 'echo'. */ private static void establishInputSource(String[] args) { if (args.length == 0) { // read input from keyboard input = new Scanner(System.in); echo = false; } else { // read input from file named args[0] try { input = new Scanner(new File(args[0])); echo = true; } catch (FileNotFoundException e) { System.out.printf("No file named %s exists; program aborting.\n", args[0]); System.exit(0); } } } }