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