Answered step by step
Verified Expert Solution
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.ioFile; Import the File class for file operations
import java.ioFileNotFoundException; Import FileNotFoundException for handling file not found exceptions
import java.ioPrintWriter; 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 mainString args throws FileNotFoundException
Scanner console new ScannerSystemin; Create a Scanner object for console input
System.out.printInput file: ;
String inputFileName console.next; Read the input file name from the console
System.out.printOutput file: ;
String outputFileName console.next; Read the output file name from the console
File inputFile new FileinputFileName; Create a File object for the input file
Scanner in new ScannerinputFile; Create a Scanner object to read from the input file
PrintWriter out new PrintWriteroutputFileName; Create a PrintWriter object to write to the output file
double total ; Initialize a variable to hold the total sum
int columnCounter ; Initialize a variable to count the columns
while inhasNextDouble Loop through the input file while there are more double values
double value innextDouble; Read the next double value from the input file
if columnCounter Check if the column is even
out.printff value; Format and write the value to the output file
else
out.printffn 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 Check if the last column was odd
out.println; Write a new line to the output file
out.printfTotal:fn total; Write the total sum to the output file
inclose; Close the input file Scanner
out.close; Close the output file PrintWriter
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started