Question
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesnt exist. You will process through the file skipping
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesnt exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of your min and max values (https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) The min/max loop (if/esle statements) may be helpful here.
import java.util.*; import java.io.*;
/** * Program reads in a file and find the max, min, sum count, and average of all * integers in the file * */ public class ProcessFile {
/** * Starts the program * * @param args array of command line arguments (not used) */ public static void main(String[] args) throws FileNotFoundException { userInterface(); }
/** * Interface with the user */ public static void userInterface() throws FileNotFoundException { Scanner console = new Scanner(System.in); Scanner fileScanner = getInputScanner(console);
// need to declare (and initalize) variables // int variables: max, min, sum, count // double variable: average
// process file // only want to examine the integers in the file
System.out.println("Maximum = " + max); System.out.println("Minimum = " + min); System.out.println("Sum = " + sum); System.out.println("Count = " + count); System.out.println("Averge = " + average); }
/** * Reads filename from user until the file exists, then return a file * scanner * * @param console Scanner that reads from the console * @return a scanner to read input from the file * @throws FileNotFoundException if File does not exist */ public static Scanner getInputScanner(Scanner console) throws FileNotFoundException { System.out.print("Enter a file name to process: "); File file = new File(console.next()); while (!file.exists()) { System.out.print("File doesn't exist. " + "Enter a file name to process: "); file = new File(console.next()); }
Scanner fileScanner = new Scanner(file); return fileScanner; } }
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