Question: Write a program in Java that reads a text file containing a list of integers. The user enters a file name for each run. Open

Write a program in Java that reads a text file containing a list of integers. The user enters a file name for each run. Open the file with Scanner. Read in the integers one by one (in a loop) and compute the average of all integers, the average of the positive integers, and the average of the negative integers. Zero is neither negative nor positive so will not be included in the average of positives nor the average of negatives. Assume that the file contains only integers (as groups of digits) and white space, no words or other characters. However, it might contain only positive integers, only negative integers, or be empty. You will need integer variables sum, count, posSum, posCount, negSum, and negCount. Initialize all these to zeMake sure that your source file is neatly indented with no tab characters. Create several data files to test your program. Test and debug with all cases. Here is a run of the program: > javac FileSums.java > java FileSums enter file name: data0.txt num: 10 num: 5 num: 8 num: 3 num: 0 num: 53 num: 77 num: 21 num: 1 num: 0 num: 3 num: 4 sum: 185 average: 15.42 sum of positives: 185 average of positives: 18.5 No negative ints in file Dont worry about how many decimal places are used to print the result (unless you want to). Another run, with an empty file: > java FileSums enter file name: dataEmpty.txt No ints in file No positive ints in file No negative ints in file > Another run: > java FileSums enter file name: dataBoth.txt num: 12 num: 34 num: 0 num: -19 num: -24 num: 45 num: 100 num: -98 num: 0 num: 65 num: 31 sum: 146 average: 13.27 sum of positives: 287 average of positives: 47.83 sum of negatives: -141 average of negatives: -47 The sensible way to write this is to first write a program that sums up all the ints in a file and computes the average. Once that is correct add logic to compute the sum of positives and their average. Once that is correct, deal with negatives.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!