Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- public class hw7countzeros {
- public static void main(String[] args) throws IOException //added throw clause
- {
- //reference to an output file
- PrintWriter outputFile = new PrintWriter("myoutput.txt");
- //declare variables
- int size;
- //create new array
- int[]numbers = new int[100];
- //call method readdata
- size = readData(numbers, outputFile);
- //call method countzeros
- int zero = countZeros(size, numbers, outputFile);
- outputFile.println("It has " + zero + " zero values");
- //call method append
- //append(size, numbers);
- outputFile.close();
- }
- /*
- * Input:
- * vals- reference to an array of integers
- * myout- reference to an PrintWriter output file
- * Process:
- * reads in integers from a inputFile and stores it in the val array
- * counts how many integers were read into the array
- * Output:
- * prints the data values read in and count to the outputFile
- * returns the array and the total numbers of value read in
- */
- public static int readData(int[]vals, PrintWriter outputFile) throws IOException {
- File inputFile = new File("C:\\Users\\KC\\eclipse-workspace\\Work\\myinput.txt");
- Scanner keyboard = new Scanner(inputFile);
- //declare variables
- int x = 0;
- //reads in integers
- while (keyboard.hasNextInt())
- {
- vals [x] = keyboard.nextInt();
- outputFile.print(vals[x] + " ");
- System.out.println(vals[x]);
- x++;
- }
- outputFile.println();
- outputFile.println("n = " + x);
- keyboard.close();
- outputFile.close();
- return x;
- }
- /*
- * Input:
- * vals - reference to an array
- * n - number of integers
- * Process:
- * counts how many first n elements of vals array are 0
- * Output:
- * return the amount of first n elements of vals array that are 0
- */
- public static int countZeros(int n, int[] vals, PrintWriter outputFile)
- {
- //declare variables
- int num = 0;
- for (int index = 0; index < n; index++) {
- if (vals[index] == 0) {
- num++;
- }
- }
- return num;
- }
- /*
- * Input:
- * vals - reference to an array
- * n - number of integers
- * Process:
- * reads in new values into array then append them at the end
- * changes value of n and the array
- * Output:
- * returns new total number of data stored in array
- */
- //public static int append(int n, int[]vals) throws Exception
- {
- //declare variable
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement