1. This programming challenge is based on Total.java program on page 515. Suppos
ID: 3712221 • Letter: 1
Question
1. This programming challenge is based on Total.java program on page 515.
Suppose you wanted to add the total to an existing file (number.txt) instead of writing a new file. As you know, you cannot simply specify the same file for input and output. When you specify an output file that exists, the file is emptied before adding new output. Sadly, that is the same file as the input file. Thus, the input file is now empty and the while loop exits immediately. Therefore no data is read.
Write 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.
2. Modify the program written in Part1 to include Exception Handling. Save the modified program as TotalModified.java.
Include the following:
-Try Statement to check if file does not exist and display an error message
-Try Statement to check if file content is an integer and display an error message if content is not an integer
_________________________________________________________________________
Total.java file
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Total
{
public static void main(String[] args) throws FileNotFoundException
{
Scanner console = new Scanner(System.in);
System.out.print("Input file: ");
String inputFileName = console.next();
System.out.print("Output file: ");
String outputFileName = console.next();
File inputFile = new File(inputFileName);
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
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();
}
}
number.txt file
32 54 67.5 29 35 80
115 44.5 100 65
Explanation / Answer
ScreenShot
---------------------------------------------------------------------------------------
1)Append an existing file using PrintWriter
//import packages for file read and write
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
//Class creation
public class TotalModified
{
//Main method
public static void main(String[] args) throws FileNotFoundException
{
//intialize total for sumof file elements
double total = 0;
//Scanner object to read user input
Scanner console = new Scanner(System.in);
//Ask user to enter input file name and read file name
System.out.print("Input file: ");
String inputFileName = console.next();
//Ask user to enter output file name and read file name
System.out.print("Output file: ");
String outputFileName = console.next();
//file object creation
File inputFile = new File(inputFileName);
//scanner object to read file
Scanner in = new Scanner(inputFile);
//create object to append in the file
PrintWriter out= new PrintWriter(new FileOutputStream(new File(outputFileName), true));
//go through each data in a file
while (in.hasNextDouble())
{
//take each element as double
double value = in.nextDouble();
//Calculate total
total = total + value;
}
//write into file
out.printf(" Total: %8.2f ", total);
//close the open files
in.close();
out.close();
}
}
----------------------------------------------------------------------------------------
2) TRY....CATCH in 1st program
//import packages for file read and write
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
//Class creation
public class TotalModified
{
//Main method
public static void main(String[] args) throws FileNotFoundException
{
//intialize total for sumof file elements
double total = 0;
//Scanner object to read user input
Scanner console = new Scanner(System.in);
//Ask user to enter input file name and read file name
System.out.print("Input file: ");
String inputFileName = console.next();
//Ask user to enter output file name and read file name
System.out.print("Output file: ");
String outputFileName = console.next();
//create object to append in the file
PrintWriter out= new PrintWriter(new FileOutputStream(new File(outputFileName), true));
//file object creation
try {
File inputFile = new File(inputFileName);
//If file name not present then generate error message
if (inputFile.exists() )
{
//scanner object to read file
Scanner in = new Scanner(inputFile);
//go through each data in a file
while (in.hasNextDouble())
{
//take each element as double
double value = in.nextDouble();
//Calculate total
total = total + value;
//If the value in file not integer indicate corresponding message
if(value!=(int)value) {
System.out.println(value +" is not integer ");
}
}
//write into file
out.printf(" Total: %8.2f ", total);
//close the open files
in.close();
out.close();
}
else
{
System.out.println("File not found");
}
}
catch(FileNotFoundException se)
{
se.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.