import java.io.IOException; import java.util.Scanner; import java.util.StringTokenizer; /** * Author by flo on 10/25/15. * Project Num : Project 4 * Purpose : To demonstrate the use of LinkedList, Nodes, Error Handling and text processing * Algorithm : using Nodes to store data then add it onto a linked list. * : First prompt a user to input a command, nothing will be stored in a node until * : an $insert has been entered. using system.err to print out all of users input errors * : onto screen and allow user to try again. $print will print all of info in each nodes in * : a linked list. $print m,n will print specific nodes. * : $delete m,n will delete as long as M < N. $line m will insert into a specific location in * : a linked list. $search M will search a specific word in each nodes in a list.$done will end * : this program * Compile : javac Project4Driver.java * : java Project4Driver * : Cant think of a better way in hacking this driver class so therefore is loooooooooooooong and crazy nested. * : i cant imagine how text processors using commands are coded such as Emacs and VIM * : even with 6 commands it became this messy, i cant imagine with just 10 commands with error handling * : and various command parameters. * : This is highly custom code, i dont think it will work in your environment, b/c i dont know how to * : write generic java data type classes where it can take in any Objects. not sure if primitive types are * : considered objects in Java but it can do auto unbox for example Integer type can be automatically converted to int. * : Not exactly sure if an int -> Integer. */ // Driver public class Project4Driver { // data members private String mess; private LinkedList ll; private static boolean isInsert = false; private static int currentLine = -1; // default constructor public Project4Driver () { ll = new LinkedList(); mess = ""; } // exit program method public void exitProgram() { System.out.println("Good Bye."); System.exit(0); } // add data and node to list method with node index public void addToListWIndex(String d, int i) { ll.insert(d, i); } // add data and node to list method public void addToList(String d) { this.mess = d; ll.insert(this.mess); } // delete node method public void deleteNode(int x, int y) { for (; x" + " Please Enter a command or a line of text: "); mess = scan.nextLine(); st = new StringTokenizer(mess, " "); token = st.nextToken(); // first token int m, n; // do once then check do { // test to see if its a command if ('$' == token.charAt(0)) { isInsert = false; // see if it will match any of these commands specifically if (token.toLowerCase().equals("$insert") || token.toLowerCase().equals("$delete") || token.toLowerCase().equals("$print") || token.toLowerCase().equals("$line") || token.toLowerCase().equals("$search") || token.toLowerCase().equals("$done")) { // Get the commands one at a time if (token.toLowerCase().equals("$insert")) { // $insert isInsert = true; p4.addToList(mess); } else if (token.toLowerCase().equals("$delete")) { // $delete m,n // get the second and third tokens, parse it then use it in a local method // to delete a range of nodes String str = st.nextToken(); if ( str.contains(",") ) { try { StringTokenizer s = new StringTokenizer(str, ","); m = Integer.parseInt(s.nextToken()); n = Integer.parseInt(s.nextToken()); if ( m < n ) { p4.deleteNode(m,n); } else { System.err.println("second value cannot be smaller than first value."); } } catch (NumberFormatException ex){ System.err.println("The values entered are not a number" + ex.getMessage()); } } } // multiple print functions else if (token.toLowerCase().equals("$print")) { // $print m,n if (!st.hasMoreTokens()) { p4.printAll(); } else { String str = st.nextToken(); if (str.contains(",")) { // split on ',' then process try { StringTokenizer s = new StringTokenizer(str, ","); m = Integer.parseInt(s.nextToken()); n = Integer.parseInt(s.nextToken()); p4.printNode(m,n); } catch (NumberFormatException ex){ System.err.println("The values entered are not a number" + ex.getMessage()); } } else { System.err.println("$print cannot take in one single value."); } } } else if (token.toLowerCase().equals("$line")) { // $line m // check to see if there is an integer tokens if (st.countTokens() == 1) { try { int line = Integer.parseInt(st.nextToken()); // catching user input errors if (line > p4.getLinkedListSize()) { System.err.println("Input (" + line + ") greater than current number of lines (" + p4.getLinkedListSize() + ")."); } else if (line < 1) { System.err.println("Input (" + line + ") must be 1 or more."); } else { // User input line to be current line currentLine = line; // showing +/- 3 lines of text for (int i = currentLine - 3; i < currentLine + 3; i++) { if (currentLine > 0 && currentLine < p4.getLinkedListSize()) { System.out.println(" > " + p4.getNodeText(i)); } } } } catch (NumberFormatException err) { System.err.println("Argument must be an integer."); } } else { System.err.println("$line must have one integer argument."); } } else if (token.toLowerCase().equals("$search")) { // $search string // search from head to toe on the text input by user token = st.nextToken().trim(); System.out.println(p4.searchNode(token)); } else if (token.toLowerCase().equals("$done")) { // $done // exit program p4.exitProgram(); } } else { // retry System.err.println("The command you have entered did not matched any of the commands we have allocated."); System.err.println("Try again.\n"); } } else if (isInsert) { // previous command was $insert and this line is not a command // does not start with '$' if ( currentLine == -1 ) { p4.addToList(mess); } else { // add a specific node to linked list with index in mind // then update counter p4.addToListWIndex(mess, currentLine); currentLine = currentLine + 1; } } else { // only text and illicit commands case // Let it fall through. System.err.println("Illicit commands and simple text, Try again.\n"); } // retry or prompt user to continue with the next line System.out.println("Line <" + p4.getLinkedListSize() + ">" + " Please Enter a command or a line of text: "); mess = scan.nextLine(); st = new StringTokenizer(mess, " "); token = st.nextToken(); // if its $done then end otherwise continue } while ( !token.trim().toLowerCase().equals("$done")); } }