Question
Include the following in the Java program: Try Statement to check if file does not exist and display an error message Try Statement to check
Include the following in the Java program:
Try Statement to check if file does not exist and display an error message
Try Statement to check if file content is an integer and display an error message if content is not an integer
___________________________________________________________________________________________
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
This program reads a file with numbers, and writes the numbers to another
file, lined up in a column and followed by their total.
*/
public class totalModified
{
public static void main(String[] args) throws FileNotFoundException
{
// Prompt for the input and output file names
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
System.out.print("Output file: ");
String outputFileName = console.next();
// Construct the Scanner and PrintWriter objects for reading and writing
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
// Read the input and write the output
double total = 0;
while (in.hasNextDouble())
{
double value = in.nextDouble();
out.printf("%15.2f ", value);
total = total + value;
}
out.printf("Total: %8.2f ", total);
in.close();
out.close();
}
}
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