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