Below are two files. The first is the basiccomputer file and the second is the v
ID: 3824443 • Letter: B
Question
Below are two files. The first is the basiccomputer file and the second is the values.txt file. The problem I am having is that the Values.txt file is not reading into the basic computer file so that when the numArray displays it does not show the correct information. Could you please tell me what I have done wrong here, so I can fix this error and obtain the correct display. The files are in the same folder so basic computer should be able to access values.txt.
Thank you!
import 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
{
final int SIZE = 100;
int[] numArray = new int [SIZE];
getArray(numArray);
showArray(numArray);
// Create the Scanner object.
Scanner keyboard = new Scanner(System.in);
}
/**
The getArray method takes the file information from Values.txt
and imports it into the numArray.
@param numArray The integer array that represents a 100 element array.
*/
public static void getArray(int[] numArray)throws IOException
{
int index = 0;
// Open the file.
File file = new File ("Values.txt");
if (!file.exists())
{
System.out.println("The file Values.txt cannot be found.");
}
//Create the Scanner object.
Scanner inputFile = new Scanner(file);
// Read the file contents into the array.
while (inputFile.hasNext() && index < numArray.length)
{
numArray[index] = inputFile.nextInt();
index++;
}
// Close the file.
inputFile.close();
}
/**
The show Array method shows the register variables and the array
on the console screen.
@param The numArray is a 100 element array.
*/
public static void showArray(int[] numArray)throws IOException
{
// Declare the Register variables
int progCounter = 0;
int accumulator = 0;
int operationCode = 0;
int operand = 0;
Scanner keyboard = new Scanner (System.in);
// Display the output for the program registers.
System.out.println(" ");
System.out.println(" Basic Computer Memory Dump");
System.out.println(" -------------------------- ");
System.out.println(" Program Counter : " + progCounter);
System.out.println(" Accumulator : " + accumulator);
System.out.println(" Operation Code : " + operationCode);
System.out.println(" Operand : " + operand + " ");
System.out.println("This is the Numbers Array ");
// Display the array contents of the numArray.
for (int index = 0; index < numArray.length; index++)
{
System.out.printf("+ %04d ", index);
}
//Create environmental space
System.out.println(" ");
}
}
Values.txt file data
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
84
104
101
32
112
114
111
106
101
99
116
32
105
115
32
102
105
110
105
115
104
101
100
46
32
78
111
119
32
119
101
32
99
97
100
32
98
114
101
97
116
104
33
93
94
95
96
97
98
99
Explanation / Answer
You are showing index of the element of numArray not numArray[index]. Just change this line.
// Display the array contents of the numArray.
for (int index = 0; index < numArray.length; index++)
{
System.out.printf("+ %04d ", numArray[index]);
}
Output:
+ 0000 + 0001 + 0002 + 0003 + 0004 + 0005 + 0006 + 0007 + 0008 + 0009 + 0010 + 0011 + 0012 + 0013 + 0014 + 0015 + 0016 + 0017 + 0018 + 0019 + 002
0 + 0021 + 0022 + 0023 + 0024 + 0025 + 0026 + 0027 + 0028 + 0029 + 0030 + 0031 + 0032 + 0033 + 0034 + 0035 + 0036 + 0037 + 0038 + 0039 + 0040 + 0
041 + 0042 + 0043 + 0044 + 0045 + 0046 + 0047 + 0048 + 0049 + 0084 + 0104 + 0101 + 0032 + 0112 + 0114 + 0111 + 0106 + 0101 + 0099 + 0116 + 0032 +
0105 + 0115 + 0032 + 0102 + 0105 + 0110 + 0105 + 0115 + 0104 + 0101 + 0100 + 0046 + 0032 + 0078 + 0111 + 0119 + 0032 + 0119 + 0101 + 0032 + 0099
+ 0097 + 0100 + 0032 + 0098 + 0114 + 0101 + 0097 + 0116 + 0104 + 0033 + 0093 + 0094 + 0095 + 0096 + 0097 + 0098 + 0099
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.