import java.awt.Color; import java.awt.Dimension; import java.io.File; import java.io.FileNotFoundException; import java.util.Calendar; import java.util.Scanner; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import org.math.plot.Plot2DPanel; public class Energy { public static void main(String[] args) throws FileNotFoundException { // choose file JFileChooser chooser = new JFileChooser(); int result; do { result = chooser.showOpenDialog(null); } while (result != JFileChooser.APPROVE_OPTION); File file = chooser.getSelectedFile(); // choose number of weeks Integer[] choices = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; Integer choice; do { choice= (Integer) JOptionPane.showInputDialog(null, "Pick the number of weeks", "Time frame", JOptionPane.QUESTION_MESSAGE, null, choices, 0); } while (choice == null); int week = choice.intValue(); // compute the start date Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.WEEK_OF_YEAR, -week); String date = String.format("%1$te-%1$tb-%1$ty", calendar.getTime()); // find line that start with the start date Scanner energy = new Scanner(file); String line; do { line = energy.nextLine(); } while (!line.startsWith(date)); // for remaining lines, collect third value final int AMOUNT = 3; StringBuffer values = new StringBuffer(); String[] part = line.split(","); values.append(part[AMOUNT]); while (energy.hasNextLine()) { line = energy.nextLine(); part = line.split(","); values.append("," + part[AMOUNT]); } // we are done with the Scanner energy.close(); // store the third values in an array part = values.toString().split(","); double[] amount = new double[part.length]; for (int i = 0; i < amount.length; i++) { amount[i] = Double.parseDouble(part[i]); } // plot the graph Plot2DPanel plot = new Plot2DPanel(); plot.addLinePlot("energy use", Color.BLUE, amount); plot.setAxisLabels("Hours", "MW"); JFrame frame = new JFrame("Ontario energy use"); final int HEIGHT = 500; final int WIDTH = 500; frame.setSize(new Dimension(HEIGHT, WIDTH)); frame.setContentPane(plot); frame.setVisible(true); } }