Question: This is my java code and I want to rewrite Total.java using the pseudocode below so that you can add total to the existing file

This is my java code and I want to rewrite Total.java using the pseudocode below so that you can add total to the existing file:

Open a scanner for the file.

For each number in the scanner

Add the number to an array.

Close the scanner.

Set total to 0.

Open a print writer for the file.

For each number in the array

Write the number to the print writer.

Add the number to total. Write total to the print writer.

Close the print writer.

Total.java

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 Total

{

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

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!