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"); } } }
eclipse-workspace - Azhar/src/TestFile txt - Eclipse IDE Eile Edit Navigate Search Project Run Window Help Quick Access TestFile.txt Testjava 1 34.77 229.22 3 31.55 432.10 526.66 621.47 /20.011 Package Explorer > Accidents Assigments Azhar JRC System Library JavaSC-1.8] SIC # (default package) > Os.java > Test.java TestFile txt > Barklards > Employee3 > Final >Hamadi > M251 > BM251TMA > M251TMAA >rov > Prev2 rev2 >rev TM105 > JRE System Library JavaSE 1.8) src (default package) > average.java > average2java ClassAverage2.java > DrawStars.java > Ellipse.java > factorialjava > Factorial orloop.java > FindTheSumnjava > FindTheSum With PublicstaticSumjava > Forloop Trainingjava TorLoop Training2java > Gradeforl.cap.java > Gradesjava > GradesFor students java > D Increment Method.java Writable Insert 7:6:47 Type here to search o ws ENG 2:13 PM UK 12/29/2020
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 with AI-Powered 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