Question: Required Skills Inventory Complete a partially implemented Java program Write a method that takes an argument and returns a value Use a FileInputStream to open

Required Skills Inventory
Complete a partially implemented Java program
Write a method that takes an argument and returns a value
Use a FileInputStream to open a file for reading
Use a Scanner to read input from a file
Use the Scanner hasNextInt or hasNextLine method to determine if there is more data in a file to read
Use a variable to accumulate a sum
Read and analyze a Java program
Problem Description and Given Info
You must complete the program that you are given below.
This program should ask the user for the name of the file that they would like to open. The program will then open the file with the user specified name, and total up the list of integers in the file. Input files will always have one integer on each line. There may be a different number of lines in each input file.
You must fill in the missing piece described in the code below.
you must write the sumFileContents method.
make no other changes to the code, and make no changes to any of the provided .dat files.
Be sure that you carefully read the code and understand how it works.
// This program should ask the user for the name of the file that they would like to open.
// The program will then open the file with the user specified name,
// and total up the list of integers in the file.
// Input files will always have one integer on each line.
// There may be a different number of lines in each input file.
// You must fill in the missing piece described in the code below.
//1) you must write the sumFileContents method.
// make no other changes to the code,
// and make no changes to any of the provided .dat files.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.IOException;
class Main {
// The scnr Scanner variable defined below will be used to collect any user input
public static Scanner scnr = new Scanner(System.in);
public static void main(String[] args) throws IOException {
String fileName = getFileName();
int sum = sumFileContents(fileName);
System.out.println("The sum is: "+ sum);
}
public static String getFileName(){
System.out.print("Enter file name: ");
return scnr.nextLine();
}
/*1) Write a public static method named sumFileContents.
This method should take one String argument (a complete file name).
This method should open the specified file.
This method should read in all of the integers in the file and add each to a total.
This method should return an int (the total of all integers in the file).
Be sure to add the 'throws IOException' clause after the parameter list.*/
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!