Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2.[4 pts) The following program will read integers from a text file called input1.txt, it then computes the sum of all the integers inside the

image text in transcribed
image text in transcribed
2.[4 pts) The following program will read integers from a text file called input1.txt, it then computes the sum of all the integers inside the file and saved the result as follows inside an output file called output.txt. (Mark your answer with the corresponding sub question's number clearly) The sum is: ?? where ?? will be replaced by the actual computation result. According to the program's description, write codes to finish the program. import java.io.*; import java.util.Scanner; public class ReadWrite TextFileExample { public static void main(String[] args) { int num, sum = 0; try { //(Q2.1) Create a File object from input1.txt // - //(Q2.2) create a Scanner object from above File object Scanner input = //--- while (input.hasNext() { //(Q2.3) read one integer from input1.txt //(Q2.4)compute the sum } input.close(); //(Q2.5) create a text file object which you will write the output to //(Q2.6) create a PrintWriter object //(Q2.7) write to above output file by using print() & println() methods //(Q2.8) close the file } catch (FileNotFoundException exception) { System.out.println ("The file" + file.getPath() + " was not found."); } }//end main() y/end class

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

Students also viewed these Databases questions