Advertisement
brilliant_moves

WeatherMonitor.java

Dec 4th, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.98 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class WeatherMonitor {
  5.  
  6.     /*
  7.     *   Program:    WeatherMonitor.java
  8.     *   Purpose:    Read temperature data from file, show daily difference in temperature
  9.     *   Creator:    Chris Clarke
  10.     *   Created:    04.12.2014
  11.     */
  12.  
  13.     /*  Note:       You need a text file called "weather.txt", containing the following:
  14.  
  15.                 16.2 23.5 19.1 7.4 22.8 18.5 -1.8 14.9
  16.  
  17.     */
  18.  
  19.     public static void main(String[] args) throws IOException, FileNotFoundException {
  20.         File f = new File("weather.txt");
  21.         Scanner filescan = new Scanner(f);
  22.         double oldTemp, newTemp=999;
  23.         double change;
  24.         boolean firstTime = true;
  25.  
  26.         while (filescan.hasNext()) {
  27.             if (firstTime) {
  28.                 oldTemp = filescan.nextDouble();
  29.                 firstTime = false;
  30.             } else {
  31.                 oldTemp = newTemp;
  32.             } // if
  33.             newTemp = filescan.nextDouble();
  34.             change = newTemp - oldTemp;
  35.             System.out.printf("%.1f to %.1f, change = %.1f%n",oldTemp,newTemp,change);
  36.         } // while
  37.     } // main()
  38. } // class WeatherMonitor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement