Please make this code easy to understand This is suppose to

Please make this code easy to understand. This is suppose to be a low level comp sci course so don\'t use advanced programming. Use try and catch statements, expections, file I/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.

Solution

import java.util.*;
import java.io.*;
public class Driver{
  
    //function to read from file and write to output file
    public static void process(Scanner input,PrintWriter output)
    {
        //read from file and write to output
         while (input.hasNextLine()) {
                String line = input.nextLine();
                if(Integer.parseInt(line)>0)
                output.println(line);
            }
    }

     public static void main(String []args){
       
        //prompt user for file names
         Scanner read=new Scanner(System.in);
        System.out.print(\"Enter input file name: \");
        String ifile=read.next();
         System.out.print(\"Enter output file name: \");
        String ofile=read.next();
      
        //create file objetcs
         File f1 = new File(ifile);
         File f2 = new File (ofile);
       
         //create file reader and writer variables
         Scanner sc=null;
         PrintWriter pw=null;
  
    //open input file pointer and throw exception if not found
         try
         {
             sc = new Scanner(f1);
        }
        catch (FileNotFoundException e)
        {
            System.out.println(\"Unable to open input file\");
        }
      
        //open output file pointer and throw exception if not found
         try
         {
             pw = new PrintWriter (f2);
             //call process function
             process(sc,pw);
           
             //close pointers
             pw.close();
             sc.close();
        }
        catch (FileNotFoundException e)
        {
            System.out.println(\"Unable to open output file\");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
     }
}

Please make this code easy to understand. This is suppose to be a low level comp sci course so don\'t use advanced programming. Use try and catch statements, ex
Please make this code easy to understand. This is suppose to be a low level comp sci course so don\'t use advanced programming. Use try and catch statements, ex

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site