For the following I have finished most of this question but

For the following I have finished most of this question but am having problem with on method. I just need the void copyNumbers method filled out. The commented out sections describe what it needs to do. I have it started. Just need it finished.

package reading_with_exceptions;

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 {

// 1.) Create a Scanner from the inputFilename. Catch exceptions from errors.
// 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
  
void process(String inputFilename) {
  
int number_to_read = 0;

try {
Scanner input = new Scanner(inputFilename);
String outputFilename = input.next();

PrintStream os = new PrintStream(new FileOutputStream(outputFilename));

if ( input.hasNextInt()) {
number_to_read = input.nextInt();
}else {
number_to_read = -1;
}


copyNumbers(input, os, number_to_read);

input.close();
os.close();

printToScreen(outputFilename);

}catch (FileNotFoundException e) {   
System.out.println(\"Cannot find file: \" + e);
return;
}
  

}

// 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
  
while (scan.hasNext()) {
  
if (numIntsToRead < 0) {
//print out a complaint message and then read as many integers as you find.
   scan.nextInt();
}
if (!scan.hasNextInt()) {
// print a complaint message and skip over the data
scan.next();
}
if ( scan < numIntsToRead) {
// complain but do not abort
}
}
}


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
}

Solution

Please follow the code and comments for description :

CODE :

import java.io.File; // required imports for the code
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Reading_With_Exceptions { // class that runs the code

// 1.) Create a Scanner from the inputFilename. Catch exceptions from errors.
// 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
void process(String inputFilename) { // method that does the processing of the file

int number_to_read = 0;
File file = new File(inputFilename); // reading it to a file as the input form is a string name

try {
String outputFilename;
PrintStream os;
try (Scanner input = new Scanner(file)) { // checking or reading the unput file
outputFilename = input.nextLine();
os = new PrintStream(new FileOutputStream(outputFilename)); // saving the firstline to a output filename
  
if (input.hasNextInt()) { // checking for the data in the file
number_to_read = input.nextInt();
} else {
number_to_read = -1;
}
copyNumbers(input, os, number_to_read);
}
os.close(); // closing the stream class
printToScreen(outputFilename); // calling the method to print the data from the output file

} catch (FileNotFoundException e) { // catching the exceptions
System.out.println(\"Cannot find file: \" + e);
}

}
// 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) { // method to copy the data to the file

// 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
while (scan.hasNext()) { //iterating over the loop to read the data

if (numIntsToRead < 0) {
//print out a complaint message and then read as many integers as you find.
System.out.println(\"A Message Indicating that reading all the data from the file.\");
while(scan.hasNextInt()){
int readNum = scan.nextInt();
ps.print(readNum);
ps.print(\"\ \");
ps.flush();
System.out.println(\"\ Completed Reading the Data.!\ \");
}
}
if (!scan.hasNextInt()) {
// print a complaint message and skip over the data
System.out.println(\"\ No Data Present in the File to Read.!\ \");
scan.nextLine();
}
}
}

public static void main(String[] args) { // driver method
Reading_With_Exceptions rwe = new Reading_With_Exceptions();
Scanner sc = new Scanner(System.in);
System.out.println(\"Enter the Number of Files : \"); // prompt for the user the enter the total number of files
int num = sc.nextInt();
System.out.println(\"Enter the FileName : \"); // prompt to enter the file name with extension

String file[] = new String[num];
sc.nextLine();
for (int x = 0; x < num; x++) {
String filename = sc.nextLine();
file[x] = filename;
}
for (String files : file) {
System.out.println(\"\ \ =========== Processing \" + files + \" ==========\ \");
rwe.process(files); //calling the method to process the data files
}
}

// For the last step, we Copy the contents of the file to the screen
private void printToScreen(String filename) { // method to print the data to the screen
Scanner scan = null;

try {
FileInputStream fis = new FileInputStream(filename);
scan = new Scanner(fis);
  
System.out.println(\"Data Present in the Output File is : \ \");
  
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
}


OUTPUT :


Enter the Number of Files :
1
Enter the FileName :
input.txt


=========== Processing input.txt ==========

A Message Indicating that reading all the data from the file.

Completed Reading the Data.!

No Data Present in the File to Read.!

Data Present in the Output File is :

102
3
4
5
6
7
8
9
10


Hope this is helpful.

For the following I have finished most of this question but am having problem with on method. I just need the void copyNumbers method filled out. The commented
For the following I have finished most of this question but am having problem with on method. I just need the void copyNumbers method filled out. The commented
For the following I have finished most of this question but am having problem with on method. I just need the void copyNumbers method filled out. The commented
For the following I have finished most of this question but am having problem with on method. I just need the void copyNumbers method filled out. The commented
For the following I have finished most of this question but am having problem with on method. I just need the void copyNumbers method filled out. The commented

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site