Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //program Kevin Chen
- //program CISC 1115 My2
- //program HW#7CountZeros
- 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");
- Scanner keyboard1 = new Scanner(new File("testing.txt"));
- //call method append
- append(size, numbers, outputFile, keyboard1);
- 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\\Kevin Chen\\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);
- 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 void append(int n, int[]vals, PrintWriter outputFile, Scanner keyboard1) throws IOException
- {
- int count=0;
- int count2 =0;
- int [] array = new int [100];
- for (int sd = 0; sd <= 3; sd++) {
- int y = keyboard1.nextInt();
- array[count]= y;
- System.out.println(array[count]);
- count++;
- }
- int [] array2 = new int [n+count];
- for(int j=0; j < count; j++){
- array2[n+count2]=array[j];
- }
- for(int i =0; i<vals.length;i++) {
- array2[i]=array[i];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement