Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have the correct output and everything, I just want to split the code into different methods. The code basically reads a file with .txt

I have the correct output and everything, I just want to split the code into different methods. The code basically reads a file with .txt file with temps on it and calculates the average, max, and min temps.

import java.io.File; import java.io.PrintStream; import java.io.FileNotFoundException; import java.util.Scanner;

public class FileIO {

public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("This program reports the following statistics about a file of Celsius temperatures:"); System.out.println("\tthe number of temperatures in the file"); System.out.println("\tthe average temperature"); System.out.println("\tthe minimum temperature");

System.out.println("\tthe maximum temperature"); System.out.print("Please enter a file name: "); String filename = console.nextLine(); File f = new File(filename); while (!f.exists()) { System.out.println("Sorry, file does not exist."); System.out.print("Please enter a file name: "); filename = console.nextLine(); f = new File(filename); } try { Scanner input = new Scanner(f); PrintStream out = new PrintStream(new File("output.txt")); int n = 0;

double temp, max_temp=0, min_temp=0, avg_temp = 0.0; while (input.hasNextDouble()) { temp = input.nextDouble(); if (n==0){ max_temp = temp; min_temp = temp; } if (max_temp max_temp = temp; if (min_temp>temp) min_temp = temp; avg_temp += temp; n++;

} avg_temp /= n; System.out.println("Number of temperatures: "+n);

System.out.printf("Average temperature: %.2f ",avg_temp);

System.out.printf("Minimum temperature: %.2f ",min_temp);

System.out.printf("Maximum temperature: %.2f ",max_temp);

} catch (FileNotFoundException e) { }

} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database And Information Systems Research Advances

Authors: Azer Bestavros ,Victor Fay-Wolfe

1st Edition

1461377803, 978-1461377801

More Books

Students also viewed these Databases questions