1 /* 2 * Program featuring an array to store and interactively manipulate a list of numbers. 3 */ 4 5 6 package arrayPlay; 7 8 import javax.swing.*; 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 23 //Establish the array of numbers 24 readNumbers(); 25 //Check the data 26 System.out.println("\nThe original list of numbers..."); 27 displayNumbers(); 28 //Enter the interpreter 29 interpreter(); 30 } catch (FileNotFoundException ex) { 31 System.out.println("The file was not found. Please think again."); 32 System.exit(-1); 33 34 } 35 } 36 private static void readNumbers () throws FileNotFoundException { 37 Scanner scanner = establishScanner("NumberSet.txt"); 38 while (scanner.hasNext()) { 39 numbers[numberOfNumbers] = scanner.nextInt(); 40 numberOfNumbers = numberOfNumbers + 1; 41 } 42 } 43 private static Scanner establishScanner (String fn) throws FileNotFoundException { 44 String separator = File.separator; 45 String homeDirectory = System.getProperty("user.home"); 46 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 47 String fullFileName = path + fn; 48 return new Scanner(new File(fullFileName)); 49 } 50 private static void interpreter () { 51 System.out.print(">>>"); 52 String command = commandReader.next(); 53 if (command.equalsIgnoreCase("DISPLAY")) { 54 interpreterDisplayCommand(); 55 } else if (command.equalsIgnoreCase("PRINT")) { 56 interpretPrintCommand(); 57 } else if (command.equalsIgnoreCase("SWAP")) { 58 interpretSwapCommand(); 59 } else if (command.equalsIgnoreCase("ADD")) { 60 interpretAddCommand(); 61 } else if (command.equalsIgnoreCase("HELP")) { 62 interpretHelpCommand(); 63 } else if (command.equalsIgnoreCase("EXIT")) { 64 System.exit(0); 65 } else { 66 System.out.println("### Unrecognizable command:" + command); 67 } 68 interpreter(); 69 } 70 71 private static void interpreterDisplayCommand() { 72 displayNumbers(); 73 } 74 75 private static void displayNumbers() { 76 for (int x= 0; x<numberOfNumbers; x=x+1){ 77 System.out.println(numbers[x]); 78 } 79 } 80 81 private static void interpretPrintCommand() { 82 String operand= commandReader.next(); 83 if (operand.equalsIgnoreCase("FIRST")) { 84 System.out.println(numbers[0]); 85 }else if (operand.equalsIgnoreCase("LAST")) { 86 System.out.println(numbers[numberOfNumbers - 1]); 87 }else { 88 int index= Integer.valueOf(operand); 89 System.out.println(numbers[index-1]); 90 } 91 } 92 private static void interpretSwapCommand() { 93 int position1= commandReader.nextInt(); 94 int position2= commandReader.nextInt(); 95 int temp= numbers[position1-1]; 96 numbers[position1-1]= numbers[position2-1]; 97 numbers[position2-1]=temp; 98 } 99 private static void interpretHelpCommand() { 100 System.out.println("HELP - display a menu of commands"); 101 System.out.println("DISPLAY - display the list of numbers"); 102 System.out.println("PRINT- print a number(FIRST; LAST; nth) "); 103 System.out.println("ADD - add a number to the list(FIRST; LAST)"); 104 System.out.println("EXIT - terminate execution of the program"); 105 } 106 private static void interpretAddCommand() { 107 String position= commandReader.next(); 108 if (position.equalsIgnoreCase("LAST")) { 109 addLast(); 110 }else if (position.equalsIgnoreCase("FIRST")) { 111 addFIRST(); 112 }else { 113 System.out.println("### invalid operand for add command"); 114 } 115 numberOfNumbers= numberOfNumbers+1; 116 } 117 private static void addLast() { 118 numbers[numberOfNumbers]= commandReader.nextInt(); 119 } 120 private static void addFIRST(){ 121 for (int x= numberOfNumbers; x>0; x=x-1){ 122 numbers[x]=numbers[x-1]; 123 } 124 numbers[0]= commandReader.nextInt(); 125 } 126 127 } 128 129 130 131 132 133