Page 1 of 1

reading_with_exceptions Create a package named "reading_with_exceptions". Write a class named: ReadingWithExceptions wit

Posted: Tue Jul 12, 2022 8:28 am
by answerhappygod
reading_with_exceptions
Create a package named "reading_with_exceptions". Write a classnamed: ReadingWithExceptions with the following method:
void process(String inputFilename)
Your program will encounter errors, and we want you togracefully handle them in such a way that you print out informativeinformation and continue executing.
Your process routine will try to open a file with the name ofinputFilename for input. If there is any problem (i.e. the filedoesn't exist), then you should catch the exception, give anappropriate error and then return. Otherwise, your program readsthe file for instructions.
Your process routine will read the first line of the filelooking for an outputFilename String followed by an integer.i.e.:outputFilename number_to_readYour program will want to write output to a file having the nameoutputFilename. Your program will try to read from "inputFilename"the number of integers found in "number_to_read".
Your process method will copy the integers read frominputFilename and write them to your output file (i.e.outputFilename). There should contain 10 numbers per line of outputin your output file.
If you encounter bad input, your program should not die with anexception. For example:
If the count of the numbers to be read is bad or < 0 you willprint out a complaint message and then read as many integers as youfind.
If any of the other numbers are bad, print a complaint messageand skip over the data
If you don't have enough input numbers, complain but do notabort
After you have processed inputFilename, I would like yourprogram to then close the output file and tell the user that thefile is created. Then Open up the output file and
copy it to the Screen.
For example, if inputFilename contained:
Page 1 of 4
Page 2 of 4 We would expect the output ofyour program to be (Note that after 23 numbers we stop
printing numbers):
The main program will access the command line to obtain the listof filenames to call your process routine with.
To prove that your program works, I want you to run your programwith the following command line parameters:file1.txt non-existent-file file2.txt file3.txtWhere non-existent-file does not exist.
file1.txt contains:
file2.txt contains:
file3.txt contains:
Can someone debug my code? It does not run at all! Thank you somuch!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.InputMismatchException;
import java.util.Scanner;
public class MP1 {
void process(String inputFilename)
{
File file = new File(inputFilename);
FileWriter fw =null;
int n = 0;
boolean result = false;
try {
Scanner sc = new Scanner(file);
try
{
String outputfilename = sc.next();
fw = new FileWriter(outputfilename);
n = sc.nextInt();
int count = 0;
while (sc.hasNextLine()) {
int i = -99;
try
{
i = sc.nextInt();
}catch(InputMismatchException e){
}
if(i!=-99)
fw.write(i+" ");
count ++;
if(count%10==0)
{
fw.write("\r\n");
}
if(count == n)
{
result = true;
break;
}
}
if(!result){
if(n<0)System.out.println("read numbers count isless than 0\n");
if(count<n)System.out.println("file numbers count isless than read numbers count\n");
}
fw.close();
printToScreen(outputfilename);
} catch (IOException e) {
System.out.println("Problem Withoutput file name");
}
sc.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public staticvoid main(String[] args)
{
MP1 mp1 = new MP1();
for (int i=0; i <args.length; i++)
{
System.out.println("\n\n===========Processing "+ args + " ==========\n");
mp1.process(args);
}
}
private voidprintToScreen(String filename)
{
Scanner scan = null;
try {
FileInputStream fis = newFileInputStream(filename);
scan = new Scanner(fis);
while (scan.hasNextLine())
{
System.out.println(scan.nextLine());
}
}
catch (FileNotFoundException e)
{
System.out.println("printToScreen:can't open: " +filename);
}
finally {
if (scan != null)
scan.close();
}
}
}