Question Solved1 Answer please help me write my outputFile properly. they are highlight red my code: import java.io.*; import java.util.Scanner; public class homework7b { public static int readData(int[] numbers, Scanner inputFile) throws IOException { //Variable to count numbers int n=0; int count=0; int [] array = new int[100]; //Create an output file object using the PrintWriter Class PrintWriter outputFile = new PrintWriter("homework7.2output.txt"); //PrintWriter oututFile = new PrintWriter(System.out); // read and count the number of marks System.out.print("Enter the marks - end with EOF: "); //Test for EOF while (inputFile.hasNext()) { numbers[n] = inputFile.nextInt(); count++; n++; } for(n=0;n<count;n++) { outputFile.print("\t"+ numbers[n]); } return count; } // countZeros() method public static int countZeros(int array[],int len){ int count=0; for(int n=0;n<len;n++) { if(array[n]==0) { count++; } } return count; } //Append() method public static int [] append(int array[],int len) throws IOException { Scanner input=new Scanner(new File("input2.txt")); int new_length=0; while(input.hasNext()) { int num = input.nextInt(); new_length++; } int [] new_array=new int[len+new_length]; for(int n=0;n<len;n++) { new_array[n]=array[n]; } input=new Scanner(new File("homework7dinput.txt")); for(int n=0;n<new_length;n++) { new_array[len+n]=input.nextInt(); } return new_array; } public static void main(String [] args) throws Exception { int [] array=new int[100]; //Open the input file File myFile = new File("homework7cinput.txt"); //Create a Scanner object to read the input file Scanner inputFile = new Scanner(myFile); //Scanner inputFile = new Scanner(System.in); //Open the input file #2 File myFile1 = new File("homework7dinput.txt"); //Create a Scanner object to read the input file Scanner inputFile1 = new Scanner(myFile1); //Scanner inputFile = new Scanner(System.in); //Create an output file object using the PrintWriter Class PrintWriter outputFile = new PrintWriter("homework7.2output.txt"); //PrintWriter oututFile = new PrintWriter(System.out); // read and count the number of marks System.out.print("Enter the marks - end with EOF: "); int len = readData (array,new Scanner(new File("homework7cinput.txt"))); print_array(array,len); outputFile.println("Number of zeroes: "+ countZeros(array,len)); array = append(array,len); print_array(array,array.length); outputFile.println("Number of zeroes: "+countZeros(array,array.length)); } //Method to print the array public static void print_array(int array[],int len)throws IOException { outputFile.print("Array: "); for(int n=0;n<len;n++) { outputFile.print(array[n]+" "); } outputFile.println(); } } Homework - Topic 7 cont. - Count Zeros: Write a complete program to do the following: (a) Write a method called readData() which receives two parameters, an array of integers called vals, which is initially uninitialized, and a reference to an output file. The method reads integers from an input file until it reaches EOF, storing the data values in the array vals. The method counts how many integers were read into the array. The method prints the data values read in and the count to an output file. The method returns the filled-in array and the total number of data values read in. (b) Write a method called countZeros() which receives two parameters, an integer n and an array of integers called vals. The method counts and returns how many of the first n elements of the vals array are 0. For example, if the array holds 66 0 4 0 4 31 with n=6, it has two 0 values. (c) Write a method called append() which reads in several new values (from a second input file) into an array of integers, appending them at the end of the array. The method receives the same two parameters as countZeros(). As a result, it must change both the array and the value of n. The method returns the updated array as well as the new total number of data values stored within the array. For example, Assume the array initially holds 66 0 4 0 4 31 with n=6; after the method call, the array might hold 66 0 -4 0 4 31 22 0 49 with n=9. (Make sure that several new ( values are added to the array.) (d) Write a main program which calls these methods. First, the main program calls readData() to read a set of data into an array called numbers, which contains no more than 100 integers. The number of elements actually read is returned by the method and stored in a variable called size. Then, the main program calls the method countZeros() to find out how many of the size array elements are 0. The main program prints this value to the output file. Next, the main program calls append() to modify the numbers array and the value of size. The append() method reads in new values from a file until input failure (i.e., EOF is reached), appending the new values to the end of the existing array and incrementing the number of values stored within the array. The complete revised array and new total count are printed (in either the main program or the method) to the output file. Finally, the main program calls the method countZeros() again to determine how many elements in the new array are 0 and prints the result to the output file.

WZMLBS The Asker · Computer Science

please help me write my outputFile properly. they are highlight red

my code:

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

public class homework7b {

public static int readData(int[] numbers, Scanner inputFile) throws IOException {

//Variable to count numbers
int n=0;
int count=0;
int [] array = new int[100];

//Create an output file object using the PrintWriter Class
PrintWriter outputFile = new PrintWriter("homework7.2output.txt");
//PrintWriter oututFile = new PrintWriter(System.out);

// read and count the number of marks
System.out.print("Enter the marks - end with EOF: ");

//Test for EOF
while (inputFile.hasNext())
{
numbers[n] = inputFile.nextInt();
count++;
n++;
}

for(n=0;n<count;n++) {
outputFile.print("\t"+ numbers[n]);
}
return count;
}

// countZeros() method
public static int countZeros(int array[],int len){
int count=0;
for(int n=0;n<len;n++)
{
if(array[n]==0)
{
count++;
}
}
return count;
}

//Append() method
public static int [] append(int array[],int len) throws IOException
{
Scanner input=new Scanner(new File("input2.txt"));
int new_length=0;
while(input.hasNext())
{
int num = input.nextInt();
new_length++;
}
int [] new_array=new int[len+new_length];
for(int n=0;n<len;n++)
{
new_array[n]=array[n];
}
input=new Scanner(new File("homework7dinput.txt"));
for(int n=0;n<new_length;n++)
{
new_array[len+n]=input.nextInt();
}
return new_array;
}

public static void main(String [] args) throws Exception {
int [] array=new int[100];

//Open the input file
File myFile = new File("homework7cinput.txt");

//Create a Scanner object to read the input file
Scanner inputFile = new Scanner(myFile);
//Scanner inputFile = new Scanner(System.in);

//Open the input file #2
File myFile1 = new File("homework7dinput.txt");

//Create a Scanner object to read the input file
Scanner inputFile1 = new Scanner(myFile1);
//Scanner inputFile = new Scanner(System.in);

//Create an output file object using the PrintWriter Class
PrintWriter outputFile = new PrintWriter("homework7.2output.txt");
//PrintWriter oututFile = new PrintWriter(System.out);

// read and count the number of marks
System.out.print("Enter the marks - end with EOF: ");

int len = readData (array,new Scanner(new File("homework7cinput.txt")));
print_array(array,len);
outputFile.println("Number of zeroes: "+ countZeros(array,len));
array = append(array,len);
print_array(array,array.length);
outputFile.println("Number of zeroes: "+countZeros(array,array.length));
}

//Method to print the array
public static void print_array(int array[],int len)throws IOException {
outputFile.print("Array: ");
for(int n=0;n<len;n++) {
outputFile.print(array[n]+" ");
}
outputFile.println();
}
}


Transcribed Image Text: Homework - Topic 7 cont. - Count Zeros: Write a complete program to do the following: (a) Write a method called readData() which receives two parameters, an array of integers called vals, which is initially uninitialized, and a reference to an output file. The method reads integers from an input file until it reaches EOF, storing the data values in the array vals. The method counts how many integers were read into the array. The method prints the data values read in and the count to an output file. The method returns the filled-in array and the total number of data values read in. (b) Write a method called countZeros() which receives two parameters, an integer n and an array of integers called vals. The method counts and returns how many of the first n elements of the vals array are 0. For example, if the array holds 66 0 4 0 4 31 with n=6, it has two 0 values. (c) Write a method called append() which reads in several new values (from a second input file) into an array of integers, appending them at the end of the array. The method receives the same two parameters as countZeros(). As a result, it must change both the array and the value of n. The method returns the updated array as well as the new total number of data values stored within the array. For example, Assume the array initially holds 66 0 4 0 4 31 with n=6; after the method call, the array might hold 66 0 -4 0 4 31 22 0 49 with n=9. (Make sure that several new ( values are added to the array.) (d) Write a main program which calls these methods. First, the main program calls readData() to read a set of data into an array called numbers, which contains no more than 100 integers. The number of elements actually read is returned by the method and stored in a variable called size. Then, the main program calls the method countZeros() to find out how many of the size array elements are 0. The main program prints this value to the output file. Next, the main program calls append() to modify the numbers array and the value of size. The append() method reads in new values from a file until input failure (i.e., EOF is reached), appending the new values to the end of the existing array and incrementing the number of values stored within the array. The complete revised array and new total count are printed (in either the main program or the method) to the output file. Finally, the main program calls the method countZeros() again to determine how many elements in the new array are 0 and prints the result to the output file.
More
Transcribed Image Text: Homework - Topic 7 cont. - Count Zeros: Write a complete program to do the following: (a) Write a method called readData() which receives two parameters, an array of integers called vals, which is initially uninitialized, and a reference to an output file. The method reads integers from an input file until it reaches EOF, storing the data values in the array vals. The method counts how many integers were read into the array. The method prints the data values read in and the count to an output file. The method returns the filled-in array and the total number of data values read in. (b) Write a method called countZeros() which receives two parameters, an integer n and an array of integers called vals. The method counts and returns how many of the first n elements of the vals array are 0. For example, if the array holds 66 0 4 0 4 31 with n=6, it has two 0 values. (c) Write a method called append() which reads in several new values (from a second input file) into an array of integers, appending them at the end of the array. The method receives the same two parameters as countZeros(). As a result, it must change both the array and the value of n. The method returns the updated array as well as the new total number of data values stored within the array. For example, Assume the array initially holds 66 0 4 0 4 31 with n=6; after the method call, the array might hold 66 0 -4 0 4 31 22 0 49 with n=9. (Make sure that several new ( values are added to the array.) (d) Write a main program which calls these methods. First, the main program calls readData() to read a set of data into an array called numbers, which contains no more than 100 integers. The number of elements actually read is returned by the method and stored in a variable called size. Then, the main program calls the method countZeros() to find out how many of the size array elements are 0. The main program prints this value to the output file. Next, the main program calls append() to modify the numbers array and the value of size. The append() method reads in new values from a file until input failure (i.e., EOF is reached), appending the new values to the end of the existing array and incrementing the number of values stored within the array. The complete revised array and new total count are printed (in either the main program or the method) to the output file. Finally, the main program calls the method countZeros() again to determine how many elements in the new array are 0 and prints the result to the output file.
See Answer
Add Answer +20 Points
Community Answer
LDCXOF The First Answerer
See all the answers with 1 Unlock
Get 4 Free Unlocks by registration

Note:&#160;To better understand the code please see the screenshot of the code at the end of the answer. If you have any doubts then please ask in comments. &#160; Java: I have edited your code and made appropriate changes. All of the code is explained in comments. &#160; Filename: Homework7b.java import java.io.*; import java.util.Arrays; import java.util.Scanner; public class Homework7b { /** * Read integers from input file and store the in array of numbers Counts how * many integers were read into the array Prints the data values read in and the * count to an output file * * @param vals numbers array * @param inputFile * @return values count * @throws IOException */ public static int readData(int[] vals, Scanner inputFile) throws IOException { // Variable to count numbers read int count = 0; // Create an output file object using the PrintWriter Class PrintWriter outputFile = new PrintWriter("homework7.2output.txt"); // While file has not reached to EOF while (inputFile.hasNext()) { // Read integer from file and store in array also increment count vals[count++] = inputFile.nextInt(); } // Write values read to output file outputFile.println("Data values read from input file: "); for (int i = 0; i &lt; count; i++) { outputFile.print(vals[i] + " "); } outputFile.println(); // Write count to output file outputFile.println("Count of numbers: " + count); // Close outputFile outputFile.close(); // Return values read return count; } /** * Counts and returns how many of the first n elements of the vals array are 0. * * @param vals Values array * @param n Number of values to read * @return number of zeros in vals array till n elements */ public static int countZeros(int[] vals, int n) { // To keep count of zero values int count = 0; // loop through n values in vals array for (int i = 0; i &lt; n; i++) { // If value is zero then increment count if (vals[i] == 0) { count++; } } // Return zeroes count return count; } /** * Read inputs from new file and append them at the end of vals array * * @param vals values array * @param n number of elements in vals array * @re ... See the full answer