1 package arrayplay; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.util.Scanner; 6 7 public class NumberList { 8 9 //VARIABLES LOCAL TO THE CLASS< AND HENCE GLOBAL TO THE METHODS 10 private static final int LIMIT = 1000; 11 private static int[] numbers = new int[LIMIT]; 12 private static int numberOfNumbers = 0; 13 private static Scanner commandReader = new Scanner(System.in); 14 15 public static void main(String[] args) { 16 try { 17 //ESTABLISH THE ARRAY OF NUMBERS 18 readNumbers(); 19 //CHECK THE DATA 20 //System.out.println("\nThe original list of numbers ..."); 21 //displayNumbers(); 22 //ENTER THE INTERPRETER 23 interpreter(); 24 } catch (FileNotFoundException ex) { 25 System.out.println("The file was not found. Please think again."); 26 System.exit(-1); 27 } 28 } 29 30 //Assuming that the data file will be found in the public_html/data 31 //Subdirectory of the user's home directory 32 private static Scanner establishScanner(String fn) throws FileNotFoundException { 33 String separator = File.separator; 34 String homeDirectory = System.getProperty("user.home"); 35 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 36 String fullFileName = path + fn; 37 System.out.println(fullFileName); 38 return new Scanner(new File(fullFileName)); 39 } 40 41 private static void readNumbers() throws FileNotFoundException { 42 Scanner scanner = establishScanner("NumberSet.txt"); 43 while (scanner.hasNext()) { 44 numbers[numberOfNumbers] = scanner.nextInt(); 45 numberOfNumbers = numberOfNumbers + 1; 46 } 47 } 48 49 private static void displayNumbers() { 50 for (int x = 0; x < numberOfNumbers; x = x + 1) { 51 System.out.println(numbers[x]); 52 } 53 } 54 55 private static void interpreter() { 56 System.out.print(">>> "); 57 String command = commandReader.next(); 58 if (command.equalsIgnoreCase("DISPLAY")) { interpreterDisplayCommand(); } 59 else if (command.equalsIgnoreCase("PRINT")) { interpreterPrintCommand(); } 60 else if (command.equalsIgnoreCase("SWAP")) { interpreterSwapCommand(); } 61 else if (command.equalsIgnoreCase("ADD")) { interpreterAddCommand(); } 62 else if (command.equalsIgnoreCase("HELP")) { interpreterHelpCommand(); } 63 else if (command.equalsIgnoreCase("EXIT")) { System.exit(0); } 64 else { System.out.println("### Unrecognizable command: " + command); } 65 66 interpreter(); 67 } 68 69 private static void interpreterDisplayCommand() { displayNumbers(); } 70 71 private static void interpreterPrintCommand() { 72 String operand = commandReader.next(); 73 if (operand.equalsIgnoreCase("FIRST")) { System.out.println(numbers[0]); } 74 else if (operand.equalsIgnoreCase("LAST")) { System.out.println(numbers[numberOfNumbers - 1]); } 75 else { 76 int index = Integer.valueOf(operand); 77 System.out.println(numbers[index - 1]); 78 } 79 } 80 81 private static void interpreterHelpCommand() { 82 System.out.println("HELP - display a menu of command"); 83 System.out.println("DISPLAY - display the list of numbers"); 84 System.out.println("PRINT - print a number (FIRST;LAST;nth)"); 85 System.out.println("SWAP - exchange two elements (nth;mth)"); 86 System.out.println("ADD - add a number to the list (FIRST;LAST)"); 87 System.out.println("EXIT - terminate execution of the program"); 88 } 89 90 private static void interpreterSwapCommand() { 91 int position1 = commandReader.nextInt(); 92 int position2 = commandReader.nextInt(); 93 int temp = numbers[position1 - 1]; 94 numbers[position1 - 1] = numbers[position2 - 1]; 95 numbers[position2 - 1] = temp; 96 } 97 98 private static void interpreterAddCommand() { 99 String position = commandReader.next(); 100 if (position.equalsIgnoreCase("LAST")) { 101 addLast(); 102 } 103 else if (position.equalsIgnoreCase("FIRST")) { 104 addFirst(); 105 } 106 else { 107 System.out.println("### invalid operand for add command"); 108 } 109 numberOfNumbers = numberOfNumbers + 1; 110 } 111 112 private static void addLast() { 113 numbers[numberOfNumbers] = commandReader.nextInt(); 114 } 115 116 private static void addFirst() { 117 for (int x = numberOfNumbers; x > 0; x = x - 1) { 118 numbers[x] = numbers[x - 1]; 119 } 120 numbers[0] = commandReader.nextInt(); 121 } 122 123 } 124 125 126 127 128 129 130 131 132 133 134