1 /* 2 * Program featuring an array to store and interactively manipulate a list of numbers. 3 */ 4 package arrayplay; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.util.Scanner; 8 public class NumberList { 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 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 // Assuming that the data file will be found in the public_html/data 29 // subdirectory of the user’s home directory. 30 private static Scanner establishScanner(String fn) throws FileNotFoundException { 31 String separator = File.separator; 32 String homeDirectory = System.getProperty("user.home"); 33 String path = homeDirectory + separator + "IdeaProjects" + separator + "CSC-212" + separator + "src" + separator; 34 String fullFileName = path + fn; 35 return new Scanner(new File(fullFileName)); 36 } 37 private static void readNumbers() throws FileNotFoundException { 38 Scanner scanner = establishScanner("NumberSet"); 39 while (scanner.hasNext()) { 40 numbers[numberOfNumbers] = scanner.nextInt(); 41 numberOfNumbers = numberOfNumbers + 1; 42 } 43 } 44 private static void displayNumbers() { 45 for (int x = 0; x < numberOfNumbers; x = x + 1) { 46 System.out.println(numbers[x]); 47 } 48 } 49 private static void interpreter() { 50 System.out.print(">>> "); 51 String command = commandReader.next(); 52 if (command.equalsIgnoreCase("DISPLAY")) { 53 interpreterDisplayCommand(); 54 } else if (command.equalsIgnoreCase("PRINT")) { 55 interpretPrintCommand(); 56 } else if (command.equalsIgnoreCase("SWAP")) { 57 interpretSwapCommand(); 58 } else if (command.equalsIgnoreCase("ADD")) { 59 interpretAddCommand(); 60 } else if (command.equalsIgnoreCase("HELP")) { 61 interpretHelpCommand(); 62 } else if (command.equalsIgnoreCase("EXIT")) { 63 System.exit(0); 64 } else { 65 System.out.println("### Unrecognizable command: " + command); 66 } 67 interpreter(); 68 } 69 private static void interpreterDisplayCommand() { 70 displayNumbers(); 71 } 72 private static void interpretPrintCommand() { 73 String operand = commandReader.next(); 74 if (operand.equalsIgnoreCase("FIRST")) { 75 System.out.println(numbers[0]); 76 } else if (operand.equalsIgnoreCase("LAST")) { 77 System.out.println(numbers[numberOfNumbers - 1]); 78 } else { 79 int index = Integer.valueOf(operand); 80 System.out.println(numbers[index - 1]); 81 } 82 } 83 private static void interpretHelpCommand() { 84 System.out.println("HELP - display a menu of commands"); 85 System.out.println("DISPLAY - display the list of numbers"); 86 System.out.println("PRINT - print a number (FIRST;LAST;nth)"); 87 System.out.println("SWAP - exchange two elements (nth;mth)"); 88 System.out.println("ADD - add a number to the list (FIRST;LAST)"); 89 System.out.println("EXIT - terminate execution of the program"); 90 } 91 private static void interpretSwapCommand() { 92 int position1 = commandReader.nextInt(); 93 int position2 = commandReader.nextInt(); 94 int temp = numbers[position1 - 1]; 95 numbers[position1 - 1] = numbers[position2 - 1]; 96 numbers[position2 - 1] = temp; 97 } 98 private static void interpretAddCommand() { 99 String position = commandReader.next(); 100 if (position.equalsIgnoreCase("LAST")) { 101 addLast(); 102 } else if (position.equalsIgnoreCase("FIRST")) { 103 addFirst(); 104 } else { 105 System.out.println("### invalid operand for add command"); 106 } 107 numberOfNumbers = numberOfNumbers + 1; 108 } 109 private static void addLast() { 110 numbers[numberOfNumbers] = commandReader.nextInt(); 111 } 112 private static void addFirst() { 113 for (int x = numberOfNumbers; x > 0; x = x - 1) { 114 numbers[x] = numbers[x - 1]; 115 } 116 numbers[0] = commandReader.nextInt(); 117 } 118 }