Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with this Java problem filling in the code where it says. Can not get

ID: 3781655 • Letter: N

Question

Need help with this Java problem filling in the code where it says. Can not get it to work at all. 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:

To prove that your program works, I want you to run your program with the following command line parameters:

file1.txt    non-existent-file    file2.txt     file3.txt

Where non-existent-file does not exist.

file1.txt contains:

file2.txt contains:

file3.txt contains:

Explanation / Answer

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Reading_With_Exceptions {

void process(String inputFilename)
{
Scanner scan = null;
String fileName = "";
try {
FileInputStream fis = new FileInputStream(inputFilename);
scan = new Scanner(fis);
int numIntsToRead = 0;
if (scan.hasNextLine())
{
String line = scan.nextLine();
String[] parts = line.split("\s+");
fileName = parts[0];
try{
numIntsToRead = Integer.parseInt(parts[1]);
}
catch (Exception e){
numIntsToRead = -1;
}
}
if (numIntsToRead < 0)
{
numIntsToRead = -1;
System.out.println("Bas value for numbers to read. Reading all numbers");
}
PrintStream ps = new PrintStream(
new FileOutputStream(fileName));
  
copyNumbers(scan, ps, numIntsToRead);
System.out.println("File : "+ fileName + " is created");
ps.close();
printToScreen(fileName);
}
catch (FileNotFoundException e)
{
System.out.println("can't open: " + inputFilename);
}
finally
{
if (scan != null)
scan.close();
  
}
}

// 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)
{
int numReadSoFar = 0;
int numbersInLine = 0;
while(scan.hasNext())
{
if (numIntsToRead > 0 && numReadSoFar >= numIntsToRead)
{
break;
}
if (scan.hasNextInt())
{
if (numbersInLine == 10)
{
ps.print(" ");
numbersInLine = 0;
}
numbersInLine++;
ps.print(scan.nextInt() + " ");
}
else {
scan.next();
}
}

if (numIntsToRead > 0 && numReadSoFar < numIntsToRead)
{
System.out.println("File has less numbers to read. Read numbers till they were there.");
}
}


public static void main(String[] args) {
Reading_With_Exceptions rwe = new Reading_With_Exceptions();
for (int i=0; i < args.length; i++)
{
System.out.println(" =========== Processing "+ args[i] + " ========== ");
rwe.process(args[i]);
}

}

// 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote