SHARE
TWEET

Untitled

a guest Nov 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //program Kevin Chen
  2. //program CISC 1115 My2
  3. //program HW#7CountZeros
  4.  
  5. import java.io.*;
  6. import java.util.Scanner;
  7.  
  8. public class hw7countzeros {
  9.  
  10.         public static void main(String[] args) throws IOException //added throw clause
  11.         {
  12.             //reference to an output file
  13.             PrintWriter outputFile = new PrintWriter("myoutput.txt");
  14.            
  15.             //declare variables
  16.             int size;
  17.            
  18.             //create new array
  19.             int[]numbers = new int[100];
  20.            
  21.             //call method readdata
  22.             size = readData(numbers, outputFile);
  23.            
  24.             //call method countzeros
  25.             int zero = countZeros(size, numbers, outputFile);
  26.             outputFile.println("It has " + zero + " zero values");
  27.            
  28.             Scanner keyboard1 = new Scanner(new File("testing.txt"));
  29.            
  30.             //call method append
  31.             append(size, numbers, outputFile, keyboard1);
  32.            
  33.             outputFile.close();
  34.            
  35.         }
  36.         /*
  37.          * Input:
  38.          * vals- reference to an array of integers
  39.          * myout- reference to an PrintWriter output file
  40.          * Process:
  41.          * reads in integers from a inputFile and stores it in the val array
  42.          * counts how many integers were read into the array
  43.          * Output:
  44.          * prints the data values read in and count to the outputFile
  45.          * returns the array and the total numbers of value read in
  46.          */
  47.        
  48.         public static int readData(int[]vals, PrintWriter outputFile) throws IOException {
  49.             File inputFile = new File("C:\\Users\\Kevin Chen\\eclipse-workspace\\Work\\myinput.txt");
  50.             Scanner keyboard = new Scanner(inputFile);
  51.            
  52.             //declare variables
  53.             int x = 0;
  54.            
  55.             //reads in integers
  56.             while (keyboard.hasNextInt())
  57.             {
  58.                 vals [x] = keyboard.nextInt();
  59.                 outputFile.print(vals[x] + " ");
  60.                 System.out.println(vals[x]);
  61.                 x++;
  62.             }
  63.             outputFile.println();
  64.             outputFile.println("n = " + x);
  65.             return x;
  66.         }
  67.         /*
  68.          * Input:
  69.          * vals - reference to an array
  70.          * n - number of integers
  71.          * Process:
  72.          * counts how many first n elements of vals array are 0
  73.          * Output:
  74.          * return the amount of first n elements of vals array that are 0
  75.          */
  76.  
  77.         public static int countZeros(int n, int[] vals, PrintWriter outputFile)
  78.         {
  79.             //declare variables
  80.             int num = 0;
  81.            
  82.             for (int index = 0; index < n; index++) {
  83.                 if (vals[index] == 0) {
  84.                     num++;
  85.                 }
  86.             }  
  87.             return num;
  88.         }
  89.         /*
  90.          * Input:
  91.          * vals - reference to an array
  92.          * n - number of integers
  93.          * Process:
  94.          * reads in new values into array then append them at the end
  95.          * changes value of n and the array
  96.          * Output:
  97.          * returns new total number of data stored in array
  98.          */
  99.        
  100.         public static void append(int n, int[]vals, PrintWriter outputFile, Scanner keyboard1) throws IOException
  101.         {
  102.            
  103.            
  104.             int count=0;
  105.             int count2 =0;
  106.             int [] array = new int [100];
  107.             for (int sd = 0; sd <= 3; sd++) {
  108.        
  109.             int y = keyboard1.nextInt();
  110.             array[count]= y;
  111.             System.out.println(array[count]);
  112.             count++;
  113.             }
  114.  
  115.            
  116.  
  117.             int [] array2 = new int [n+count];
  118.             for(int j=0; j < count; j++){
  119.                 array2[n+count2]=array[j];
  120.             }
  121.             for(int i =0; i<vals.length;i++) {
  122.                 array2[i]=array[i];
  123.             }
  124.            
  125.         }
  126. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top