Good evening everyone I have been absent from class for a wh
Good evening everyone! I have been absent from class for a while and I am very confused with how to do this programming assignment for computer science. If anyone can help me I would greatly appreciate it! The assignment is described below:
\"Write a Java 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.\"
ALSO, the gitlab repository for this assignment included the following format for the code:
package edu.wit.cs.comp1000;
import java.io.PrintWriter;
import java.util.Scanner;
public class PA9a {
/**
* Error to output when a file cannot be opened.
*/
static final String E_NOT_FOUND = \"Error! File not found!\";
/**
* Reads all integers in input scanner,
* outputs positive ones to output each on
* its own line
*
* @param input input source
* @param output output destination
*/
public static void process(Scanner input, PrintWriter output) {
// TODO: write your code here
}
/**
* Program execution point:
* input an input file name and an output file name,
* for each positive number in the input file
* print on its own line to the output file
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
// TODO: write your code here
}
}
Thank you all so much in advance!!
Solution
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class PA9a {
/**
* Error to output when a file cannot be opened.
*/
static final String E_NOT_FOUND = \"Error! File not found!\";
/**
* Reads all integers in input scanner,
* outputs positive ones to output each on
* its own line
*
* @param input input source
* @param output output destination
*/
public static void process(Scanner input, PrintWriter output) {
while(input.hasNextInt()){
int n = input.nextInt();
if(n>0){
output.write(n+\" \");
}
}
input.close();
output.close();
}
/**
* Program execution point:
* input an input file name and an output file name,
* for each positive number in the input file
* print on its own line to the output file
*
* @param args command-line arguments (ignored)
*/
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println(\"Enter Input File Name :\");
String inputFileName = sc.nextLine();
System.out.println(\"Enter Output File Name :\");
String outputFileName = sc.nextLine();
Scanner input;
PrintWriter output;
try {
input = new Scanner(new FileReader(inputFileName));
output = new PrintWriter(outputFileName);
PA9a.process(input, output);
} catch (FileNotFoundException ex) {
System.out.println(E_NOT_FOUND);
}
}
}

