Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(JAVA)Plot Weather Data(from txt file) using JFreeChart For this assignment, it

ID: 3830005 • Letter: #

Question

(JAVA)Plot Weather Data(from txt file) using JFreeChart

For this assignment, it requires to read in a data file containing weather observations for Anchorage, parse the contents of the file, and produce a plot.

Here is my stupid code:

package chartdemo;

import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ChartDemo extends ApplicationFrame {
String[] split,split2;
String filename ="ANC_Obs.txt";
public ChartDemo(String title) {
super(title);
TimeSeries heights = new TimeSeries("MinTemperature");
try{
FileInputStream inputStream = new FileInputStream(filename);
Scanner input = new Scanner(new FileInputStream(filename));
String line;
input.nextLine();
while (input.hasNextLine()) {
line = input.nextLine();
split = line.split(", ");
split2=split[0].split("-");
Day day = new Day(Integer.parseInt(split2[2]),Integer.parseInt(split2[1]),Integer.parseInt(split2[0]));
int minTemp=Integer.parseInt(split[2]);
heights.add(day,minTemp);
}
  
}catch(FileNotFoundException e)
{
System.out.println("Problem opening the file "+filename);
}

TimeSeriesCollection timeCollection = new TimeSeriesCollection();
timeCollection.addSeries(heights);
JFreeChart heightChart = ChartFactory.createXYLineChart("Anchorage Min Temperatures",
"Year", "Temperature(F)", timeCollection);
DateAxis dateAxis = new DateAxis();
dateAxis.setDateFormatOverride(new SimpleDateFormat("yyyy"));
heightChart.getXYPlot().setDomainAxis(dateAxis);

ChartPanel chartPanel = new ChartPanel(heightChart);
chartPanel.setPreferredSize(new java.awt.Dimension(1000, 500));
setContentPane(chartPanel);
}

  
public static void main(String[] args) {
ChartDemo chart = new ChartDemo("Anchorage Min Temperatures");
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart);
chart.setVisible(true);
}
  
}

Here is some data from ANC_Obs.txt file(The whole file has more than 20,000 data, so I only post a few here, it should work the same):

Date, MaxTemperature, MinTemperature, Precipitation, Snowfall, SnowDepth
1953-11-01, 26, 7, 0.00, 0.0, T
1953-11-02, 29, 18, 0.00, 0.0, T
1953-11-03, 27, 7, 0.00, 0.0, T
1953-11-04, 23, 2, 0.00, 0.0, T
1953-11-05, 23, 2, 0.00, 0.0, T
1953-11-06, 21, -0, T, T, T
1953-11-07, 16, 1, T, T, T
1953-11-08, 18, 1, T, T, T
1953-11-09, 23, -0, T, T, T
1953-11-10, 29, 22, 0.00, 0.0, 0
1953-11-11, 33, 27, T, 0.0, 0
1953-11-12, 33, 24, T, 0.0, 0
1953-11-13, 32, 27, T, T, 0
1953-11-14, 35, 28, 0.00, 0.0, 0
1953-11-15, 30, 21, T, T, 0
1953-11-16, 31, 19, 0.04, 0.6, 1
1953-11-17, 37, 27, 0.00, 0.0, 1
1953-11-18, 33, 15, T, T, 1
1953-11-19, 20, 10, 0.00, 0.0, 1
1953-11-20, 21, 11, 0.00, 0.0, T
1953-11-21, 22, 7, T, T, T
1953-11-22, 25, 19, 0.02, 0.4, 1
1953-11-23, 28, 16, 0.00, 0.0, 1
1953-11-24, 32, 18, 0.05, 1.0, 1
1953-11-25, 32, 17, T, T, 2
1953-11-26, 31, 21, T, T, 2
1953-11-27, 29, 12, 0.00, 0.0, 2
1953-11-28, 28, 16, T, T, 1
1953-11-29, 27, 12, 0.00, 0.0, 1
1953-11-30, 22, 14, 0.00, 0.0, 1
1953-12-01, 21, 10, 0.00, 0.0, 1
1953-12-02, 18, 2, 0.00, 0.0, 1
1953-12-03, 13, -0, T, T, T
1953-12-04, 17, 4, 0.00, 0.0, T
1953-12-05, 12, -4, T, T, T
1953-12-06, 6, -10, T, T, T
1953-12-07, 18, -3, T, T, T
1953-12-08, 25, 15, 0.01, 0.1, T
1953-12-09, 25, 18, 0.12, 3.2, 1
1953-12-10, 24, 12, T, T, 3
1953-12-11, 25, 17, 0.09, 1.0, 3
1953-12-12, 25, 7, 0.06, 1.5, 5
1953-12-13, 16, -12, 0.00, 0.0, 5
1953-12-14, 20, -13, T, T, 5
1953-12-15, 31, 18, T, T, 5
1953-12-16, 40, 27, T, T, 4
1953-12-17, 35, 27, 0.06, T, 3
1953-12-18, 32, 18, 0.00, 0.0, 3
1953-12-19, 28, 11, 0.00, 0.0, 3
1953-12-20, 20, 5, T, T, 3
1953-12-21, 36, 19, 0.21, 2.5, 3
1953-12-22, 41, 21, T, T, 6
1953-12-23, 31, 14, T, T, 4
1953-12-24, 36, 20, T, T, 4
1953-12-25, 29, 22, T, T, 4
1953-12-26, 29, 24, 0.09, 3.9, 7
1953-12-27, 29, 22, 0.26, 3.9, 7
1953-12-28, 23, 13, 0.03, 0.7, 10
1953-12-29, 22, 17, 0.11, 1.8, 12
1953-12-30, 22, 11, 0.01, 0.2, 12
1953-12-31, 21, 10, 0.06, 1.2, 13
1954-01-01, 14, 3, 0.00, 0.0, 12
1954-01-02, 14, -12, 0.00, 0.0, 11
1954-01-03, 15, -6, 0.00, 0.0, 11
1954-01-04, 12, 5, 0.00, 0.0, 11
1954-01-05, 12, -5, T, T, 11
1954-01-06, 18, -5, T, T, 11
1954-01-07, 27, 10, T, T, 10
1954-01-08, 32, 26, 0.34, 4.4, 12
1954-01-09, 36, 24, 0.03, 0.2, 14
1954-01-10, 31, 20, 0.01, 0.4, 12
1954-01-11, 30, 23, 0.01, 0.5, 13
1954-01-12, 32, 16, T, T, 13
1954-01-13, 17, -0, 0.00, 0.0, 13
1954-01-14, 18, 3, T, T, 12
1954-01-15, 19, 1, T, T, 12
1954-01-16, 12, -1, 0.00, 0.0, 12
1954-01-17, 10, -4, T, T, 12
1954-01-18, 20, -4, 0.01, 0.1, 12
1954-01-19, 26, 13, 0.00, 0.0, 12
1954-01-20, 21, -6, 0.00, 0.0, 11
1954-01-21, 10, -8, T, T, 11
1954-01-22, 8, -11, 0.00, 0.0, 11
1954-01-23, 21, -6, T, T, 11
1954-01-24, 19, 5, 0.00, 0.0, 11
1954-01-25, 20, -10, 0.00, 0.0, 11
1954-01-26, 15, -14, 0.00, 0.0, 11
1954-01-27, 11, -15, 0.00, 0.0, 11
1954-01-28, 1, -22, 0.00, 0.0, 10
1954-01-29, 7, -8, T, T, 10
1954-01-30, 16, 4, 0.08, 1.5, 11
1954-01-31, 31, 15, 0.08, 1.0, 12
1954-02-01, 30, 14, T, T, 12
1954-02-02, 32, 13, T, T, 12
1954-02-03, 39, 16, T, T, 11
1954-02-04, 26, 14, T, T, 10
1954-02-05, 41, 26, T, T, 10
1954-02-06, 38, 26, T, T, 9
1954-02-07, 30, 15, T, 0.1, 8
1954-02-08, 28, 2, 0.02, 0.1, 8
1954-02-09, 14, -4, 0.00, 0.0, 8
1954-02-10, 12, -10, 0.00, 0.0, 8
1954-02-11, 14, -9, T, T, 8
1954-02-12, 23, -9, T, T, 8
1954-02-13, 18, -5, 0.00, 0.0, 8
1954-02-14, 8, -18, 0.00, 0.0, 8
1954-02-15, 8, -20, 0.00, 0.0, 8
1954-02-16, M, -20, T, T, 8
1954-02-17, 11, -22, 0.00, 0.0, 8
1954-02-18, 8, -20, 0.00, 0.0, 8
1954-02-19, 9, -9, T, T, 8
1954-02-20, 11, -14, T, T, 8
1954-02-21, 9, -14, 0.03, 0.7, 8
1954-02-22, 14, -10, 0.13, 1.6, 10
1954-02-23, 11, -13, T, T, 10
1954-02-24, 21, 9, T, T, 10
1954-02-25, 15, -7, 0.00, 0.0, 10
1954-02-26, 1, -22, 0.00, 0.0, 10
1954-02-27, 9, -25, 0.00, 0.0, 10
1954-02-28, 14, -13, 0.00, 0.0, 10
1954-03-01, 36, 13, 0.08, 1.1, 9
1954-03-02, 34, 26, 0.10, 2.1, 12
1954-03-03, 29, 18, T, T, 11
1954-03-04, 24, 15, 0.02, 0.5, 11

My code works fine for the minimum temperature graph. My question is: if I want to change my min temperature graph to max temperature graph, such like using split[1]instead of split[2], it will show an error: java.lang.NumberFormatException: For input string: "M". And the reason is because there is a string "M" in the series of max temperature data(for example 1954-02-16, M, -20, T, T, 8). How can I catch this string and generate the integer plot for maximum temperature?

Explanation / Answer

try handling the exception,

try

{

................

}catch (NumberFormatException ex){

.....................//handle your exception here

}