/* Marty Stepp, CSE 142, Spring 2010 Reads a file of temperatures and outputs the difference between each pair of neighboring days. Input file weather.txt: 16.2 23.5 19.1 7.4 22.8 18.5 -1.8 14.9 Expected output: 16.2 to 23.5, change = 7.3 23.5 to 19.1, change = -4.4 19.1 to 7.4, change = -11.7 7.4 to 22.8, change = 15.4 22.8 to 18.5, change = -4.3 18.5 to -1.8, change = -20.3 -1.8 to 14.9, change = 16.7 */ import java.io.*; // for File import java.util.*; public class Temperatures { public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("weather.txt")); // fencepost - pull out a "post" (reading a number) double prev = input.nextDouble(); // 16.2, 23.5, 19.1, ... while (input.hasNextDouble()) { // read a temperature from the file and process it double next = input.nextDouble(); // 23.5, 19.1, 7.4, ... double change = next - prev; System.out.println(prev + " to " + next + ", change = " + change); prev = next; // prev = 23.5 } } }