Write a program called WeatherReporter.java that prompts the user for an input f
ID: 3733077 • Letter: W
Question
Write a program called WeatherReporter.java that prompts the user for an input file name. reads the given file, which is in the format of TexasWeather2010.txt, line by line and uses the processLine() method as defined below:
to output to the console the data for each day formatted as shown below:
Only allowed two use the following import statements:
import java.util.*;
import java.io.*;
TexasWeather2010.txt:
Should be in the format of:
YYYYMMDD Average(F) High(F) Low(F) Fog/Rain/Snow/Hail/Thunder/Tornado
20100101 44.2 48 34 100000
20100102 29.9 48 24.1 000000
20100103 24.2 32 18 000000
20100104 26.2 36 17.1 000000
20100105 26.9 36 17.1 000000
20100106 29.1 41 18 000000
20100107 32.4 48 19 000000
20100108 35.1 48 20.1 010000
20100109 25.9 35.6 19.4 000000
20100110 28 37.4 19.4 000000
20100111 27.6 45 15.1 000000
20100112 34.9 45 16 000000
20100113 31.4 44.6 19.4 000000
20100114 37.5 57.2 24.8 000000
20100115 42.9 63 25.2 000000
20100116 44 63 28.2 000000
20100117 53.6 68 30.2 010000
20100118 50.9 68 39.9 100000
20100119 47.8 66.2 35.6 000000
20100120 51.2 66 35.1 000000
20100121 40.2 64.9 37 010000
20100122 37.9 41 37 010000
...etc.
Explanation / Answer
Here is the complete program:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;
public class WeatherReporter {
public static void processFile(String fileName) {
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
processLine(line);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException pe) {
pe.printStackTrace();
}
}
public static void processLine(String line) throws ParseException {
String[] tokens = line.split("\s+"); // Split by whitespaces [space, tab etc]
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyymmdd");
SimpleDateFormat outputFormat = new SimpleDateFormat("mm/dd/yyyy");
String outputDate = outputFormat.format(inputFormat.parse(tokens[0]));
String low = tokens[3];
String high = tokens[2];
String snow = tokens[4].charAt(2) == '1' ? "yes": "no";
String rain = tokens[4].charAt(1) == '1' ? "yes": "no";
StringBuilder output = new StringBuilder();
output.append(outputDate).append(" ")
.append("Low: ").append(low).append(" ")
.append("High: ").append(high).append(" ")
.append("Rain: ").append(rain).append(" ")
.append("Snow: ").append(snow);
System.out.println(output);
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the file: ");
String input = sc.nextLine();
processFile(input);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.