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

Complete the public method named \"readWriteLine (Scanner in, Printwriter out)\"

ID: 3824813 • Letter: C

Question

Complete the public method named "readWriteLine (Scanner in, Printwriter out)". This method should read each line from the input scanner (in) until the scanner has no more lines to read and call another private method named "boolean processLine (String line)" (the above written method). If the result of the "processLine" method is true, then it writes that line to the output (out) stream. It throws IOException if any general IO exception happens during reading and writing from and to the streams. It also throws NumberFormatExceptrion returned by "processLine" method. public static void readWriteLine(Scanner in, PrintWriter out) throws lOException, NumberFormatException{/* While there are more lines available to read from the scanner Read a line form the input scanner Pass that line to the "processLine" method. If the result is true, then write that line into the output stream;/*//Your code goes here;}//end of readWriteLine method In the last part, write a main method that opens input and output flies, and calls the readWriteLine () method, and handles appropriate exceptions accordingly. Your program should show appropriate message for the following exceptional conditions and terminate appropriately: "File does not exist" will be shown when the input file is not found. "General I/O error" will be shown when there is a problem with IO. "Invalid number format" will be shown if a line contain anything other than two integers separated by spaces. public static void main(String!) args) {//Your code goes here;}//end of main method)//end of the Exam class

Explanation / Answer

Hi, Please find my implementation.

Part 1)

public static void readWriteLine(Scanner in, PrintWriter out) throws IOException, NumberFormatException{

      

       String line;

      

       while(in.hasNextLine()){

           line = in.nextLine();

          

           if(processLine(line))

               out.write(line);

       }

   }

Part 2)

public static void main(String[] args) {

      

       Scanner in = new Scanner(System.in);

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

       String input = in.next();

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

       String output = in.next();

      

       try{

       // opening input and output files

       Scanner sc = new Scanner(new File(input));

       PrintWriter out = new PrintWriter(new File(output));

      

       readWriteLine(sc, out);

      

       }catch (FileNotFoundException e) {

           System.out.println("File does not exist");

       }catch(NumberFormatException e){

           System.out.println("Invalid number format");

       }catch(IOException e){

           System.out.println("General I/O error");

       }

   }

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