Loading...
Loading...
A weather station logs n temperature readings, but a faulty sensor sometimes writes ERR, -- or other junk. Readings can have a decimal part and can be negative.
Use Double.parseDouble and skip every reading it cannot convert. Print how many readings were valid and bad. If at least one was valid, also print the highest and lowest with 1 decimal place and the average with 2 decimal places; otherwise print No valid readings.
Valid: V and Bad: B, then either Highest: H, Lowest: L, Average: A or No valid readings.NaN, 1e3 or 5dExample 1
Input
6 31.5 ERR 29.0 33.5 -- 30
Output
Valid: 4 Bad: 2 Highest: 33.5 Lowest: 29.0 Average: 31.00
(31.5 + 29 + 33.5 + 30) / 4 = 31.
Example 2
Input
3 NA error 4O
Output
Valid: 0 Bad: 3 No valid readings
Try the problem first. Open one hint at a time.
Read each reading with sc.next() and convert it inside a try in the loop.
Start the highest and lowest from the first valid reading, not from 0: all readings may be negative.
Use printf with %.1f and %.2f for the decimals.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor