Question: 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

 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

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

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 Databases Questions!