Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class hw7countzeros {
  5.  
  6. public static void main(String[] args) throws IOException //added throw clause
  7. {
  8. //reference to an output file
  9. PrintWriter outputFile = new PrintWriter("myoutput.txt");
  10.  
  11. //declare variables
  12. int size;
  13.  
  14. //create new array
  15. int[]numbers = new int[100];
  16.  
  17. //call method readdata
  18. size = readData(numbers, outputFile);
  19.  
  20. //call method countzeros
  21. int zero = countZeros(size, numbers, outputFile);
  22. outputFile.println("It has " + zero + " zero values");
  23.  
  24. //call method append
  25. //append(size, numbers);
  26.  
  27. outputFile.close();
  28. }
  29. /*
  30. * Input:
  31. * vals- reference to an array of integers
  32. * myout- reference to an PrintWriter output file
  33. * Process:
  34. * reads in integers from a inputFile and stores it in the val array
  35. * counts how many integers were read into the array
  36. * Output:
  37. * prints the data values read in and count to the outputFile
  38. * returns the array and the total numbers of value read in
  39. */
  40.  
  41. public static int readData(int[]vals, PrintWriter outputFile) throws IOException {
  42. File inputFile = new File("C:\\Users\\KC\\eclipse-workspace\\Work\\myinput.txt");
  43. Scanner keyboard = new Scanner(inputFile);
  44.  
  45. //declare variables
  46. int x = 0;
  47. //reads in integers
  48. while (keyboard.hasNextInt())
  49. {
  50. vals [x] = keyboard.nextInt();
  51. outputFile.print(vals[x] + " ");
  52. System.out.println(vals[x]);
  53. x++;
  54. }
  55. outputFile.println();
  56. outputFile.println("n = " + x);
  57. keyboard.close();
  58. outputFile.close();
  59. return x;
  60. }
  61. /*
  62. * Input:
  63. * vals - reference to an array
  64. * n - number of integers
  65. * Process:
  66. * counts how many first n elements of vals array are 0
  67. * Output:
  68. * return the amount of first n elements of vals array that are 0
  69. */
  70.  
  71. public static int countZeros(int n, int[] vals, PrintWriter outputFile)
  72. {
  73. //declare variables
  74. int num = 0;
  75.  
  76. for (int index = 0; index < n; index++) {
  77. if (vals[index] == 0) {
  78. num++;
  79. }
  80. }
  81. return num;
  82. }
  83. /*
  84. * Input:
  85. * vals - reference to an array
  86. * n - number of integers
  87. * Process:
  88. * reads in new values into array then append them at the end
  89. * changes value of n and the array
  90. * Output:
  91. * returns new total number of data stored in array
  92. */
  93.  
  94. //public static int append(int n, int[]vals) throws Exception
  95. {
  96. //declare variable
  97.  
  98.  
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement