Question

In: Computer Science

Read the input one line at a time until you have read all lines. Now output...

Read the input one line at a time until you have read all lines. Now output these lines in the opposite order from which they were read..

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Part12 {
  
   /**
   * Your code goes here - see Part0 for an example
   * @param r the reader to read from
   * @param w the writer to write to
   * @throws IOException
   */
   public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
       // Your code goes here - see Part0 for an example
   }

   /**
   * The driver. Open a BufferedReader and a PrintWriter, either from System.in
   * and System.out or from filenames specified on the command line, then call doIt.
   * @param args
   */
   public static void main(String[] args) {
       try {
           BufferedReader r;
           PrintWriter w;
           if (args.length == 0) {
               r = new BufferedReader(new InputStreamReader(System.in));
               w = new PrintWriter(System.out);
           } else if (args.length == 1) {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(System.out);              
           } else {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(new FileWriter(args[1]));
           }
           long start = System.nanoTime();
           doIt(r, w);
           w.flush();
           long stop = System.nanoTime();
           System.out.println("Execution time: " + 1e-9 * (stop-start));
       } catch (IOException e) {
           System.err.println(e);
           System.exit(-1);
       }
   }
}

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Part0 {
  
   /**
   * Read lines one at a time from r. After reading all lines, output
   * all lines to w, outputting duplicate lines only once. Note: the order
   * of the output is unspecified and may have nothing to do with the order
   * that lines appear in r.
   * @param r the reader to read from
   * @param w the writer to write to
   * @throws IOException
   */
   public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
Set<String> s = new HashSet<>();

for (String line = r.readLine(); line != null; line = r.readLine()) {
s.add(line);
}

for (String text : s) {
w.println(text);
}
   }

   /**
   * The driver. Open a BufferedReader and a PrintWriter, either from System.in
   * and System.out or from filenames specified on the command line, then call doIt.
   * @param args
   */
   public static void main(String[] args) {
       try {
           BufferedReader r;
           PrintWriter w;
           if (args.length == 0) {
               r = new BufferedReader(new InputStreamReader(System.in));
               w = new PrintWriter(System.out);
           } else if (args.length == 1) {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(System.out);              
           } else {
               r = new BufferedReader(new FileReader(args[0]));
               w = new PrintWriter(new FileWriter(args[1]));
           }
           long start = System.nanoTime();
           doIt(r, w);
           w.flush();
           long stop = System.nanoTime();
           System.out.println("Execution time: " + 1e-9 * (stop-start));
       } catch (IOException e) {
           System.err.println(e);
           System.exit(-1);
       }
   }
}


Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Part1 {

        public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
                String line;

                // create a linked list whose elements are String objects
                LinkedList<String> allLines = new LinkedList<String>();

                // read the file line by line
                while((line = r.readLine()) != null) {

                        // remove whitespaces from the line
                        line = line.trim();

                        // if line has characters after removing whitespaces
                        // add the line to the linked list
                        if(!line.isEmpty()){
                                allLines.add(line + "\n");
                        }
                }

                // print the lines in reverse order
                // i.e. traverse the linked list in reverse order
                Iterator it = allLines.descendingIterator();
                
                // while there are more elements
                while(it.hasNext()) {
                        // write the element to stream
                        w.write((String)it.next());
                }
        }

        public static void main(String[] args) {
                try {
                        BufferedReader r;
                        PrintWriter w;

                        if (args.length == 0) {
                                r = new BufferedReader(new InputStreamReader(System.in));
                                w = new PrintWriter(System.out);
                        } 
                        else if (args.length == 1) {
                                r = new BufferedReader(new FileReader(args[0]));
                                w = new PrintWriter(System.out);                

                        } 
                        else {
                                r = new BufferedReader(new FileReader(args[0]));
                                w = new PrintWriter(new FileWriter(args[1]));
                        }

                        long start = System.nanoTime();
                        doIt(r, w);
                        w.flush();
                        long stop = System.nanoTime();

                        System.out.println("Execution time: " + 1e-9 * (stop-start));

                } 
                catch (IOException e) {
                        System.err.println(e);
                        System.exit(-1);
                }
        }
}

Related Solutions

Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
Read in user-input integers one at a time until the user types ‘q’, storing all inputs...
Read in user-input integers one at a time until the user types ‘q’, storing all inputs in a list. Then, print out the even numbers in increasing order, from smallest value to largest. Python
An input transmission line of characteristic impedance Z01 splits off into three identical output transmission lines...
An input transmission line of characteristic impedance Z01 splits off into three identical output transmission lines having characteristic impedance Z02 = 3Z01. This represents a four-port system. Find all 16 of the S parameters for this system. Note: Many of the 16 Sij parameters will be identical. There should be only four values that are distinct. SHOW WORK!!
You can use any language. You have to provide all the data, output and input. You...
You can use any language. You have to provide all the data, output and input. You are to use Linked Lists to do this Program. The XYZ Widget store receives shipments of widgets at various costs. The store’s policy is to charge a 30% markup, and to sell widgets which were received earlier before widgets which were received later. This is called a FIFO policy. Write a program using linked lists that reads in three types of input data and...
Write a batch program that takes and echoes input data one character at a time until...
Write a batch program that takes and echoes input data one character at a time until EOF is encountered and then prints a summary such as The 14 lines of text processed contained 20 capital letters, 607 lowercase letters, and 32 punctuation marks. Program: C
Now that you have read about the various social theories of aging, which one do you...
Now that you have read about the various social theories of aging, which one do you think represents aging in the most effective and comprehensive way? What effect does aging have on cognitive function and intelligence? What types of leisure, spiritual and/or civic activities can older adults continue to engage that support the aging process?
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
Write a program that echoes the input one word per line. Remove all punctuation and all...
Write a program that echoes the input one word per line. Remove all punctuation and all blank lines. Code must be written in C. Not c++ or c#. Input: Use the following Ogden Nash poem to test your program. The Parsnip The parsnip, children, I repeat, Is simply an anemic beet. Some people call the parsnip edible; Myself, I find this claim incredible Note: There are multiple blank lines before and after the title. There may be multiple blank spaces...
You have $100,000 now and will receive $130,000 in exactly one years’ time. If you spend...
You have $100,000 now and will receive $130,000 in exactly one years’ time. If you spend $80,000 today and market interest rates are 10% p.a. compounded semi-annually, how much will you have in one year? a) $130,000 b) $152,050 c) $150,000 d) $240,000 e) $240,250 Mr Simon de Money wishes to purchase a new Mercedes Benz C250 CDI. The car costs $73,200. Mr de Money has arranged a loan that only covers part of the purchase price. He intends to...
Instructions Today, all input and output has been taken care of for you. All you need...
Instructions Today, all input and output has been taken care of for you. All you need to do is finish off the function wordCount that calculates the number of words in a string. Function Details Input Input has been handled for you. A string will be read in. Processing Complete the wordCount function. Note that you have been given Java comments describing its expected parameters and return value. /** * wordCount (String) -> int * Calculates the number of words...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT