1. This programming challenge is based on Total.java program on page 515 (Note:
ID: 3712127 • Letter: 1
Question
1. This programming challenge is based on Total.java program on page 515 (Note: Total.java and number.txt are in a zipped folder Chapter11-Example1 provided in Sakai) 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:
1. Try Statement to check if file does not exist and display an error message
2. Try Statement to check if file content is an integer and display an error message if content is not an integer
Explanation / Answer
Total .java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Total {
private static final int MAX_SIZE = 1000; //Maximum size of array
public static void main(String[] args) {
// Open a Scanner for the file
File file = new File("./numbers.txt");
try {
Scanner sc = new Scanner(file);
//Declare an array
int [] array=new int[MAX_SIZE];
int i=1;
//Add Data to array
while(sc.hasNextLine())
{
array[i-1]=sc.nextInt();
i++;
}
//close the scanner
sc.close();
int total=0; //Declare Total
//Open Print Writer for the file
PrintWriter writer=new PrintWriter(file);
//Write each number to array
for (int j : array) {
if(j!=0)
{
writer.write(j+" ");
System.out.println("Number is : "+j);
total+=j;
}
}
//Write total to File
writer.write(total+"");
System.out.println("Total is : "+total);
//Close the PrintWriter
writer.close();
} catch (FileNotFoundException e) {
System.out.println("File Not Found ");
e.printStackTrace();
}
}
}
Output:
Console :
Number is : 15
Number is : 22
Number is : 11
Number is : 44
Number is : 55
Number is : 87
Number is : 65
Number is : 77
Number is : 66
Number is : 40
Number is : 25
Number is : 14
Number is : 10
Number is : 14
Number is : 30
Number is : 50
Number is : 60
Number is : 70
Number is : 62
Number is : 40
Total is : 857
TotalModified.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class TotalModified {
private static final int MAX_SIZE = 1000; // Maximum size of array
public static void main(String[] args) {
// Open a Scanner for the file
File file = new File("./numbers.txt");
Scanner sc = null;
try {
sc = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("File Not Found ");
e.printStackTrace();
}
// Declare an array
int[] array = new int[MAX_SIZE];
int i = 0;
// Add Data to array
while (sc.hasNextLine()) {
try // Exception Handling
{
array[i] = sc.nextInt();
i++;
} catch (NumberFormatException e) {
System.out.println("Invalid input !!!! Number is not an integer ...");
}
}
// close the scanner
sc.close();
int total = 0; // Declare Total
// Open Print Writer for the file
PrintWriter writer = null;
try {
writer = new PrintWriter(file);
} catch (FileNotFoundException e) {
System.out.println("File Not Found ");
e.printStackTrace();
}
// Write each number to array
for (int j : array) {
if (j != 0) {
writer.write(j + " ");
System.out.println("Number is : " + j);
total += j;
}
}
// Write total to File
writer.write(total + "");
System.out.println("Total is : " + total);
// Close the PrintWriter
writer.close();
}
}
output:
Console:
Number is : 15
Number is : 22
Number is : 11
Number is : 44
Number is : 55
Number is : 87
Number is : 65
Number is : 77
Number is : 66
Number is : 40
Number is : 25
Number is : 14
Number is : 10
Number is : 14
Number is : 30
Number is : 50
Number is : 60
Number is : 70
Number is : 62
Number is : 40
Number is : 857
Number is : 1714
Total is : 3428
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.