Question: This is part of the program for my Java Final Project. The testArray portion that addresses the haltCode doesn't work. Currently it halts all of

This is part of the program for my Java Final Project. The testArray portion that addresses the haltCode doesn't work. Currently it halts all of the test files, even the ones that do not contain 50 in the elements. I am attaching the test file that contains 1 fifty, so if the program works it should kick out. Can you please tell me what I am doing wrong. The other program checks for the test files perform properly with their elements it is just this one that is giving me trouble. Thanks for your input.

java.io.*; import java.util.Scanner;

// Declare the class/namespace public class BasicComputer {

// Declare the main method public static void main(String[] args)throws IOException { // create arrays final int SIZE = 100; int[] numArray = new int[SIZE]; int[] operationCodes = {6, 7, 8, 9, 10, 11, 20, 21, 25, 26, 27, 28, 30, 31, 32, 33, 35, 40, 41, 42, 43, 50}; // Create the Scanner object. Scanner keyboard = new Scanner(System.in); setArray(numArray); testArray(numArray); showArray(numArray); operationCodes(operationCodes); sumArray(numArray); System.out.println(); numbersSay(numArray); System.out.println(" "); }

/** The setArray method takes the file information from Values.txt and imports it into the numArray. It is a void method. @param numArray The numArray is a 100 element integer array. */ public static void setArray(int[] numArray)throws IOException { // Create scanner object. Scanner keyboard = new Scanner(System.in); // Create variables. int index = 0; String filename; // Read the file contents into the array. System.out.print("Please enter the name of your test program to begin: "); filename = keyboard.nextLine(); // Open the file. File file = new File (filename); if (!file.exists()) { System.out.println("The file cannot be found."); System.exit(0); } //Create the Scanner object. Scanner inputFile = new Scanner(file); while (inputFile.hasNext() && index < numArray.length) { numArray[index] = inputFile.nextInt(); index++; } // Close the file. inputFile.close(); } /** The test Array method tests the array to make sure the elements do not overflow the array and checks to make sure the Array elements are in the range of -9999 and +9999. @param numArray The numArray is a 100 element integer array. */ public static void testArray(int[] numArray)throws IOException { final int SIZE = 100; int index; int haltValue = 50; boolean haltCode = false; // Read the file contents into the array. for(index = 0; index < SIZE; index++) { if(numArray[index] == haltValue) { haltCode = true; } if (numArray[index] < -9999 || numArray[index] > 9999) { System.out.println(" A number below -9999 or above 9999 was found in the test file."); System.out.println(" ---Basic Computer Execution Terminated---"); System.exit(0); } if (haltCode = true) { System.out.println(" The Halt Operations Code (50) has been entered."); System.out.println(" ---Basic Computer Execution Terminated---"); System.exit(0); } if (index > numArray.length) { System.out.println(" The test file is larger than the array. This does not compute Will Robinson."); System.out.println(" ---Basic Computer Execution Terminated---"); System.exit(0); } } }

}

This is the text file that contains 50 (it's element 33)

3100 2131 2132 21 34 35 36 33 32 31 30 21 33 21 20 11 0 6 7 8 9 10 43 42 41 40 25 26 27 0 125 100 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!