Please write comments and explaining the code This code shou

Please write comments and explaining the code. This code should be very basic becuase this is a low level java course, so you are limited to methodes, if-else statements, for and while loops, try and catch statements, arrays, expections, and fileI/O.

Write a program that reads a stream of integers from a file and writes only the positive numbers to a second file. The user should be prompted to enter the names of both the input file and output file in main(), and then main() should attempt to open both files (providing an error if there is an error during this process). The main() method should then call the process() method to read all the integers from the input file and write only the positive integers to the output file. The process() method takes as arguments a Scanner to read from the input and a PrintWriter to write to the output. You can assume that if you are able to successfully open the input file, then there will only be integers in it.

Please refer to this example code and you can base off of it.

\"Write a program that copies the contents of one file into another file. In particular, ask the user for the names of both the original (input) file and the new (output) file. Write a method that is passed the already created Scanner and PrintWriter objects to do all of the copying (reading and writing).

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Scanner;

public class ClassExamples {

public static void main(String[] args) {

Scanner keyboardInput = new Scanner(System.in);

System.out.print(\"Enter the input file name: \");

String inputFileName = keyboardInput.next();

System.out.print(\"Enter the output file name: \");

String outputFileName = keyboardInput.next();

try (

Scanner fin = new Scanner(new File(inputFileName));

PrintWriter fout = new PrintWriter(new File(outputFileName));

) {

copyFile(fin, fout);

}

catch (FileNotFoundException ex) {

System.out.println(\"File not found!\");

System.exit(0);

}

}

public static void copyFile(Scanner original, PrintWriter copy) {

while(original.hasNextLine()) {

String line = original.nextLine();

copy.println(line);

}

}

}

Solution

Hi,

I have modified the code. It is working as expected now.

ClassExamples.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ClassExamples {
public static void main(String[] args) {
Scanner keyboardInput = new Scanner(System.in);
System.out.print(\"Enter the input file name: \");
String inputFileName = keyboardInput.next();
System.out.print(\"Enter the output file name: \");
String outputFileName = keyboardInput.next();
try {
Scanner fin = new Scanner(new File(inputFileName));
PrintWriter fout = new PrintWriter(new File(outputFileName));
process(fin, fout);
}
catch (FileNotFoundException ex) {
System.out.println(\"File not found!\");
System.exit(0);
}
}
public static void process(Scanner original, PrintWriter copy) {
while(original.hasNextLine()) {
String line = original.nextLine();
if(Integer.parseInt(line) > 0){
copy.println(line);
}
}
copy.flush();
copy.close();
}

}

Output:

Enter the input file name: D:\\\\1.txt
Enter the output file name: D:\\\\2.txt

1.txt

1
2
3
4
-5
-6
7
8
9

2.txt

1
2
3
4
7
8
9

Please write comments and explaining the code. This code should be very basic becuase this is a low level java course, so you are limited to methodes, if-else s
Please write comments and explaining the code. This code should be very basic becuase this is a low level java course, so you are limited to methodes, if-else s
Please write comments and explaining the code. This code should be very basic becuase this is a low level java course, so you are limited to methodes, if-else s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site