Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program ( Total . java ) :WHY IS THERE A SPACE IN MY OUTPUT? This is my code. import java.io . File; / / Import

Program (Total. java) :WHY IS THERE A SPACE IN MY OUTPUT? This is my code.
import java.io.File; // Import the File class for file operations
import java.io.FileNotFoundException; // Import FileNotFoundException for handling file not found exceptions
import java.io.PrintWriter; // Import PrintWriter for writing to files
import java.util.Scanner; // Import Scanner for reading input
public class Total {// Define a public class named Total
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in); // Create a Scanner object for console input
System.out.print("Input file: ");
String inputFileName = console.next(); // Read the input file name from the console
System.out.print("Output file: ");
String outputFileName = console.next(); // Read the output file name from the console
File inputFile = new File(inputFileName); // Create a File object for the input file
Scanner in = new Scanner(inputFile); // Create a Scanner object to read from the input file
PrintWriter out = new PrintWriter(outputFileName); // Create a PrintWriter object to write to the output file
double total =0; // Initialize a variable to hold the total sum
int columnCounter =0; // Initialize a variable to count the columns
while (in.hasNextDouble()){// Loop through the input file while there are more double values
double value = in.nextDouble(); // Read the next double value from the input file
if (columnCounter %2==0){// Check if the column is even
out.printf("%8.2f", value); // Format and write the value to the output file
} else {
out.printf("%10.2f%n", value); // Format and write the value to the output file with a new line
}
columnCounter++; // Increment the column counter
total += value; // Add the value to the total sum
}
if (columnCounter %2!=0){// Check if the last column was odd
out.println(); // Write a new line to the output file
}
out.printf("Total:%13.2f%n", total); // Write the total sum to the output file
in.close(); // Close the input file Scanner
out.close(); // Close the output file PrintWriter
}
}
image text in transcribed

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

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions

Question

analyze file formats and basic digital design rules.

Answered: 1 week ago