I tried looking to find a solution but could not see anything. It might be that I am looking up the wrong thing since I not really sure what is happening. (new to java) Anyways my assignment is to: Write a console program in a class named Weather that reads an input file of temperatures, with real numbers representing daily high temperatures. Your program should prompt for the file name to read, then read its data and print the change in temperature between each pair of neighboring days. Here is what I wrote
import java.io.*;
import java.util.*;
public class Weather {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.print("Input file? ");
String fileName = input.nextLine();
Scanner file = new Scanner(new File(fileName));
double first = file.nextDouble();
while (file.hasNext()) {
if (file.hasNextDouble()) {
double second = file.nextDouble();
double diff = second - first;
System.out.println(first + " to " + second + ", change = " + diff);
first = second;
} else {
file.next();
}
}
}
}
Test two throws an error, when it is asked to do 2.7-4.5 it gives -1.799999... instead of the wanted and expected -1.8. (see photo) When I tried to fix this by changing the system.out.print to " + Math.round(diff)); it still gave the wrong answer. Any suggestions or ideas what I should search to find the answer on this forum. test 2 throws an error