Home / Expert Answers / Computer Science / 11-10-zylab-addition-using-files-file-1-additionhandler-java-import-java-util-scanner-import-pa530

(Solved): 11.10 **zyLab: Addition using Files File 1 - AdditionHandler.java import java.util.Scanner; import ...



11.10 **zyLab: Addition using Files

The goal of this problem is to read \( n \) integers from a file, calculate their sum, and then print the sum. You are expectSum is : 74
3. Handle an exception.
If the name provided by the user is a file name that does not exist, instead of returningFile 1 - AdditionHandler.java

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;

public class AdditionHandler {

public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
boolean isFileRead = false;
// TODO
// 1. Prompt user for filename
// 2. Call readFile method with that filename
// 3. if file does not open catch the exception and prompt
// user for another file name
}

/**
* This method opens the file with name fileName, reads the integers,
* calculates and prints their sum and returns true.
* If file is not found it will throw IOException.
*
* @param fileName String argument of name of file to be opened for reading
* @return true if file is opened
* @throws IOException if the file isn't found
*/
private static boolean readFile(String fileName) throws IOException {
// TODO
return false;
}
}

File 2 - integers.txt

12 23 34 45 56

The goal of this problem is to read integers from a file, calculate their sum, and then print the sum. You are expected to handle any exceptions thrown when opening and reading from a file. You will prompt a user for the name of a file and use that name to open a file. If the name given is a valid name the program should print 'Valid File', read the integers, and sum them. Otherwise, it will throw an exception and you will be in charge of handling it. In the case of a valid file name, after calculating the sum of the integers, print the result. The integers will be in the first line of the file, separated by a single white space. (Eg. 2645390 1) Below are the detailed steps with examples: 1. Prompt the user for the name of a file to read from. Use a scanner to prompt the user for the name of a file as shown below. Example output: What is the name of the file you want to open and read? Example user input: integers.txt 2. Attempt to open and read the file. Use the provided method header for readFile to fill in the code for the method. The file name will be passed into the method as the parameter. You should first open the file and then read in the list of integers and return the sum. If these steps are completed, you are done. However, if the file does not open, then the method should throw an exception, which will be handled in the main method. (Note: We assume the integers are all positive and will add up to give a sum that is positive). Example user input: integers.txt Sum is : 74 3. Handle an exception. If the name provided by the user is a file name that does not exist, instead of returning the sum of integers, the readFile method will throw an exception. It is your job to handle this exception by telling the user what happened and prompting for another file name as shown. Example output: What is the name of the file you want to open? Example user input: test1.txt Example output: File name is invalid! What is the name of the file you want to open? Note: You are provided with a test file "integers.txt". If using Eclipse/IntelliJ, put the txt file in the project folder, not inside the src folder. This will allow you to put the filename without specifying a path in the program. Before turning it in, remember to Style and Comment following the course standards in the CS 200 Style Guide. Created by Abirami Vijay. Reviewed by Tiger.


We have an Answer from Expert

View Expert Answer

Expert Answer



Here's the implementation of the AdditionHandler program that prompts the user for the name of a file, reads n integers from the file, calculates their sum, and then prints the sum. The implementation handles any exceptions thrown when opening and reading from a file.





We have an Answer from Expert

Buy This Answer $5

Place Order

We Provide Services Across The Globe