Question: 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
}
}
 Program (Total. java) :WHY IS THERE A SPACE IN MY OUTPUT?

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!