Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

mport java.io . File; import java.io . FileNotFoundException; import java.io . PrintWriter; import java.util.Scanner; / * * This program reads a file with numbers, and

mport 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 two columns, and followed by the 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();
String outputFileName = "out.txt";
// 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("%8.2f
", value(0,5),value(5,5));
total = total + value;
}
out.printf("Total: %-8.2f
", total);
in.close();
out.close();
}
}

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

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions