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

Objectives Overall Lab 13 Instructions Using the instructions from Closed Lab 01

ID: 3554414 • Letter: O

Question

Objectives

Overall Lab 13 Instructions

Using the instructions from Closed Lab 01, create a new folder named ClosedLab13.  Unlike with previous labs, you will need to import the following file into your new lab folder.  Follow the instructions from Clsoed Lab 01 to import this file into your ClosedLab13 folder.

import java.io.*;

import java.util.*;

public class Lab13a {

    

    public static void main(String[] args) {

        //Fill in the body

    }

    

    // Given a Scanner as input prompts the user to enter a file name.  If given an

    // empty line, respond with an error message until the user enters a non-empty line.

    // Return the string to the calling program.  Note that this method should NOT try

    // to determine whether the file name is an actual file - it should just get a

    // valid string from the user.

    private static String getFileName(Scanner inScanner) {

    }

    

    // Given a String as input return the reverse of that String to the calling program.

    private static String reverse(String inString) {

    

    }

}

In addition, you will need to place the following file in your ClosedLab13 folder to use as input for these exercises:

Jabberwocky

'Twas brillig, and the slithy toves

Did gyre and gimble in the wabe;

All mimsy were the borogoves,

And the mome raths outgrabe.

"Beware the Jabberwock, my son!

The jaws that bite, the claws that catch!

Beware the Jubjub bird, and shun

The frumious Bandersnatch!"

He took his vorpal sword in hand:

Long time the manxome foe he sought

Explanation / Answer

Lab13a.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lab13a {

  

    public static void main(String[] args) {
      
       Scanner inScanner = new Scanner(System.in);
        System.out.println("Enter Input filer name");
       String fileName = getFileName(inScanner);
       try {

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

                while (sc.hasNextLine()) {
                    String inString = sc.nextLine();
                    System.out.println(reverse(inString));
                }
              
            }
            catch (FileNotFoundException e) {
              // e.printStackTrace();
                System.out.println("There was a problem reading from: " + fileName);
            }

    }

  

    // Given a Scanner as input prompts the user to enter a file name. If given an

    // empty line, respond with an error message until the user enters a non-empty line.

    // Return the string to the calling program. Note that this method should NOT try

    // to determine whether the file name is an actual file - it should just get a

    // valid string from the user.

    private static String getFileName(Scanner inScanner) {
       String name = inScanner.nextLine();
       if(name == null || name.equals("")) {
           System.out.println("File name cannot be blank ");
           getFileName(inScanner);
       }
       return name;
    }

  

    // Given a String as input return the reverse of that String to the calling program.

    private static String reverse(String inString) {
       StringBuilder sb = new StringBuilder(inString);
       return sb.reverse().toString();

    }

}

Lab13b.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lab13b {

  

    public static void main(String[] args) {
      
       Scanner inScanner = new Scanner(System.in);
        System.out.println("Enter Input filer name");
       String fileName = getFileName(inScanner);
       String inString = "";
       try {

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

                while (sc.hasNextLine()) {
                    inString += sc.nextLine() +" ";
                  
                }
                System.out.println(reverse(inString));
              
            }
            catch (FileNotFoundException e) {
              // e.printStackTrace();
                System.out.println("There was a problem reading from: " + fileName);
            }

    }

  

    // Given a Scanner as input prompts the user to enter a file name. If given an

    // empty line, respond with an error message until the user enters a non-empty line.

    // Return the string to the calling program. Note that this method should NOT try

    // to determine whether the file name is an actual file - it should just get a

    // valid string from the user.

    private static String getFileName(Scanner inScanner) {
       String name = inScanner.nextLine();
       if(name == null || name.equals("")) {
           System.out.println("File name cannot be blank ");
           getFileName(inScanner);
       }
       return name;
    }

  

    // Given a String as input return the reverse of that String to the calling program.

    private static String reverse(String inString) {
       StringBuilder sb = new StringBuilder(inString);
       return sb.reverse().toString();

    }

}

Lab13c.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Lab13c {

   public static void main(String[] args) {

       Scanner inScanner = new Scanner(System.in);
       System.out.println("Enter Input file name");
       String inFileName = getFileName(inScanner);
       System.out.println("Enter output file name");
       String outFileName = getFileName(inScanner);
       if (inFileName.equals(outFileName)) {
           System.out
                   .println("ERROR! Your input file and output file MUST be different.");
           return;
       }
       String inString = "";
       FileOutputStream fop = null;
       File outFile;
       try {

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

           while (sc.hasNextLine()) {
               inString += sc.nextLine() + " ";

           }

           outFile = new File("c:/newfile.txt");
           fop = new FileOutputStream(outFile);

           // if file doesnt exists, then create it
           if (!outFile.exists()) {
               System.out.println("Error writing to file: " + outFileName);
           }

           // get the content in bytes
           byte[] contentInBytes = reverse(inString).getBytes();

           fop.write(contentInBytes);
           fop.flush();
           fop.close();

           System.out.println("Done");

       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           try {
               if (fop != null) {
                   fop.close();
               }
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

   }

   // Given a Scanner as input prompts the user to enter a file name. If given
   // an

   // empty line, respond with an error message until the user enters a
   // non-empty line.

   // Return the string to the calling program. Note that this method should
   // NOT try

   // to determine whether the file name is an actual file - it should just get
   // a

   // valid string from the user.

   private static String getFileName(Scanner inScanner) {
       String name = inScanner.nextLine();
       if (name == null || name.equals("")) {
           System.out.println("File name cannot be blank ");
           getFileName(inScanner);
       }
       return name;
   }

   // Given a String as input return the reverse of that String to the calling
   // program.

   private static String reverse(String inString) {
       StringBuilder sb = new StringBuilder(inString);
       return sb.reverse().toString();

   }

}