Question
TestFile contains 7 day weather forecast. Write a complete java program to read the temperatures from the file, (10 marks) make sure to handle the
- TestFile contains 7 day weather forecast.
- Write a complete java program to read the temperatures from the file, (10 marks) make sure to handle the error if the file was not found. (4 marks)
- Store the temperatures in a 1 Dimensional array (4 marks)
- Find the highest and lowest temperatures of the week, and print them (12 marks) I made the TestFile.txt but I got a error: java.io.FileNotFoundException: TestFile.txt (The system cannot find the file specified) : Error File not found. I have the code:
import java.util.*; import java.io.*; public class Test{
public static void main(String[] args) {
try{ File tf = new File("TestFile.txt"); Scanner scn = new Scanner(tf); Double [] tempArr = new Double[7]; int index=0;
while(scn.hasNextLine()){ tempArr[index] = scn.nextDouble(); index++; }
Double highestTemp = tempArr[0], lowestTemp = tempArr[0]; for(int i =0;i tempArr[i]){ lowestTemp = tempArr[i]; } } System.out.println("Highest temperature = "+highestTemp); System.out.println("Lowest temperature = "+lowestTemp); }
catch(FileNotFoundException exc){ System.out.println(exc + " : Error File not found"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started