185k views
5 votes
Write a program that processes a file using Java stream processing. The file is a text file containing lines of the form:

271.72 296.33 47.70 231.61
That is, each line of the file contains four positive double values. Unfortunately, mistakes in the collection and production of the file are possible: their may be non-positive values in the file and the file may contain values that are not numeric.

Step 1: Write a stream processing that produces a List from the file Input2.txt that contains only numeric, positive values.
Use the Files.lines() and Paths.get() to open and read the file to a stream.
Use the Stream.map() method to spit the lines into an array of four strings containing the values.
Use the Stream.filter() method to pass only arrays that only have strings that represent double values (use the String.matches() method in the filter.
Use the Stream.map() method to convert each array of string into an array of Doubles.
Use the Stream.filter() method to pass only arrays that have all positive values.
Use the Steam.collect() method to collect the arrays into a list. The Collectors.toList() can do this.
After the stream has produced the list, display it. Use Arrays.toString() to display each array:

List contains:
[271.72, 296.33, 47.7, 231.61]
[84.85, 208.83, 77.3, 259.89]
.
.
.
Submit your code to pass the first test.

Step 2: Now write the stream processing code that takes your List and produces an List that contains the average of each of the arrays of Double.
Use the .stream() method to turn the List into a stream.
Use the Stream.map() method to calculate the average of each array in the stream.
Use the Stream.collect() method to collect the averages into a list.
After the Stream has produces the list, display it (use System.out.printf(%.2f%n", a) to show only two decimal places):

List contains the averages
211.84
157.72
251.07
166.17
.
.
.
Submit your code to pass the second test.

Step 3) Write a stream that produces the overall average from your List.
Use the stream() method to create the stream from your List.
Use the reduce() method to accumulate the sum of your values. Use the reduce(T identity, BinaryOperator) overloading.
Use the stream() method to create a second stream from your List and use the Stream.count() method to count the number of elements in the stream.
Divide the sum from 2. by the count from 3. to get the overall average. Display the overall average using System.out.printf("%.2f%n", a)
Overall average: 199.36
Submit your code to pass the third test.

Step 4) Now that you have a List of averages and the overall average, write a last stream that produces only the averages fro your List that more than 25% above the overall average. Collect these averages in another List Display this the highest averages using System.out.printf("%.2f%n", a):

Highest averages:
251.07
295.13
256.29
262.44
.
.
.
Submit your code to pass the fourth test.

User Abhimaan
by
8.2k points

1 Answer

2 votes

Final answer:

The Java program provided covers all four steps for processing a text file using Java stream processing to filter out non-numeric and non-positive numbers, calculate averages, identify the overall average, and then extract values significantly higher than the average.

Step-by-step explanation:

The task is to write a program using Java stream processing to filter, process, and display data from a text file. Initially, each line of the file, which consists of four positive double values, is to be read and split into an array. Only numeric, positive values should be retained. Step 1 requires filtering out non-numeric and non-positive numbers, resulting in a list of arrays of valid Doubles. Step 2 involves computing and collecting the averages of these arrays into a new list, and Step 3 requires calculating the overall average of these averages. Finally, Step 4 filters the averages list to include only those more than 25% above the overall average, effectively identifying the highest averages.

Here is the Java code encompassing all four steps:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamProcessor {

public static void main(String[] args) throws Exception {
// Step 1
List list = Files.lines(Paths.get("Input2.txt"))
.map(line -> line.split("\\s+"))
.filter(arr -> Arrays.stream(arr).allMatch(s -> s.matches("\\d+(\\.\\d+)?")))
.map(arr -> Arrays.stream(arr).map(Double::valueOf).toArray(Double[]::new))
.filter(arr -> Arrays.stream(arr).allMatch(d -> d > 0))
.collect(Collectors.toList());

list.forEach(arr -> System.out.println("List contains: " + Arrays.toString(arr)));

// Step 2
List averages = list.stream()
.map(arr -> Arrays.stream(arr).mapToDouble(Double::doubleValue).average().orElse(0))
.collect(Collectors.toList());

averages.forEach(a -> System.out.printf("%.2f%n", a));

// Step 3
double sum = averages.stream().reduce(0.0, Double::sum);
long count = averages.stream().count();
double overallAverage = sum / count;
System.out.printf("Overall average: %.2f%n", overallAverage);

// Step 4
double threshold = overallAverage * 1.25;
List highestAverages = averages.stream()
.filter(a -> a > threshold)
.collect(Collectors.toList());

System.out.print("Highest averages: ");
highestAverages.forEach(a -> System.out.printf("%.2f%n", a));
}
}
User Ravnur
by
8.6k points