Need help with this Java problem filling in the code where it says. Thank You. C
ID: 3781209 • Letter: N
Question
Need help with this Java problem filling in the code where it says. Thank You.
Create a package named "reading_with_exceptions". Write a class named: Reading_With_Exceptions with the following method:
void process(String inputFilename)
Your program will encounter errors, and we want you to gracefully handle them in such a way that you print out informative information and continue executing.
Your process routine will try to open a file with the name of inputFilename for input. If there is any problem (i.e. the file doesn't exist), then you should catch the exception, give an appropriate error and then return. Otherwise, your program reads the file for instructions.
Your process routine will read the first line of the file looking for an outputFilename String followed by an integer. i.e.:
outputFilename number_to_read
Your program will want to write output to a file having the name outputFilename. 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 from inputFilename and write them to your output file(i.e. outputFilename). There should contain 10 numbers per line of output in your output file.
If you encounter bad input, your program should not die with an exception. For example:
If the count of the numbers to be read is bad or < 0 you will print out a complaint message and then read as many integers as you find.
If any of the other numbers are bad, print a complaint message and skip over the data
If you don't have enough input numbers, complain but do not abort
After you have processed inputFilename, I would like your program to then close the output file and tell the user that the file is created. Then Open up the output file and copy it to the Screen.
For example, if inputFilename contained:
We would expect the output of your program to be (Note that after 23 numbers we stop printing numbers):
The main program will access the command line to obtain the list of filenames to call your process routine with.
For those of you who benefit from a Template, your program might look like:
Explanation / Answer
package com.et.reading_with_exceptions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Reading_With_Exceptions {
void process(String inputFilename)
{
Scanner scanner=null;
// Here is where your work goes ... Steps that you will need to do:
// 1.) Create a Scanner from the inputFilename. Catch exceptions from errors.
try{
scanner=new Scanner(new File(inputFilename));
while(scanner.hasNext()){
String line1 = scanner.next();
System.out.println(line1);
}
}catch(InputMismatchException e)
{
e.printStackTrace();;
}
catch(Exception e)
{
System.out.println("file not found");
main(null);
}
// 2.) Read the first String from the file and use it to create a PrintStream
// catching appropriate exceptions
// 3.) Using hasNextInt and nextInt, carefully read the count integer.
// I recommend -1 for a count value if it is bad to indicate reading ALL
// 4.) Use copyNumbers method described below to complete the job
// 5.) Close Scanner and PrintStream objects
// 6.) Call printToScreen to copy the output file to the screen
// The following routine is called to complete the job of copying integers to
// the output file:
// scan - a Scanner object to copy integers from
// ps - A PrintStream to write the integers to
// numIntsToRead - number of integers to read. A value of -1 ==> read all integers
}
void copyNumbers(Scanner scan, PrintStream ps, int numIntsToRead)
{
// hasNext() can be used to see if the scan object still has data
// Note that hasNextInt() can be used to see if an integer is present
// nextInt() will read an integer
// next() can be used to skip over bad integers
}
public static void main(String[] args) {
Reading_With_Exceptions rwe = new Reading_With_Exceptions();
System.out.println("enter the file name");
Scanner scanner=new Scanner(System.in);
String filename=scanner.nextLine();
rwe.process(filename);
}
// For the last step, we Copy the contents of the file to the screen
private void printToScreen(String filename)
{
Scanner scan = null;
try {
FileInputStream fis = new FileInputStream(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();
}
}// end of printToScreen
} // end of class
output
enter the file name
D:\inputfile1.txt
25
36
14
25
69
87
12
54
8
7
20
1
1
2
3