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: datatxt num: num: num: num: num: num: num: num: num: num: num: num: sum: average: sum of positives: average of positives: 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: num: num: num: num: num: num: num: num: num: num: sum: average: sum of positives: average of positives: sum of negatives: average of negatives: 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
