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

This program will read a text file. Each line in this text file must be stored i

ID: 3891664 • Letter: T

Question

This program will read a text file. Each line in this text file must be stored in a Queue. You will then create an iterator to walk through Queue. For each String, you will use a recursive method to reverse the String. After reversing the String, store it in a Stack. Pop the lines from the Stack and write them to a file as well as the console.  

Create a new exception that will be thrown if the user enters an invalid file name.

Sample Output:

Sets - store a group of nonduplicate elements

Lists - store an ordered collection of elements

Stacks - store objects that are processed in a last-in, first-out fashion

Queues - store objects that are processed in a first-in first-out fashion

Priority Queues - store objects that are processed in the order of their priorities

text file is

seitiroirp rieht fo redro eht ni dessecorp era taht stcejbo erots - seueuQ ytiroirP
noihsaf tuo-tsrif ni-tsrif a ni dessecorp era taht stcejbo erots - seueuQ
noihsaf tuo-tsrif ,ni-tsal a ni dessecorp era taht stcejbo erots - skcatS
stnemele fo noitcelloc deredro na erots - stsiL
stnemele etacilpudnon fo puorg a erots - steS

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// QueueStackDemo.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.Iterator;

import java.util.PriorityQueue;

import java.util.Queue;

import java.util.Scanner;

import java.util.Stack;

public class QueueStackDemo {

                /**

                * a simple recursive method to reverse a String

                *

                * @param str

                *            -input string

                * @param index

                *            - starting index (should be 0 at beginning)

                * @return reversed string

                */

                static String reverse(String str, int index) {

                                /**

                                * if the string is empty or null, returning empty string

                                */

                                if (str == null || index >= str.length()) {

                                                return "";

                                }

                                if (index == str.length() - 1) {

                                                // last character

                                                return "" + str.charAt(index);

                                }

                                // returning the characters in reverse order

                                return reverse(str, index + 1) + str.charAt(index);

                }

                public static void main(String[] args) throws FileException {

                                Scanner scanner = new Scanner(System.in);

                                // getting input file name

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

                                String file = scanner.nextLine();

                                // defining input file

                                File input = new File(file);

                                if (!input.exists()) {

                                                /**

                                                * throwing newly created exception for file not found

                                                */

                                                throw new FileException("File not found!");

                                }

                                // defining output file

                                File output = new File("output.txt");

                                /**

                                * creating a stack and queue

                                */

                                Stack<String> stack = new Stack<String>();

                                Queue<String> queue = new PriorityQueue<String>();

                                try {

                                                /**

                                                * Reading the input file and filling the queue

                                                */

                                                Scanner fileScanner = new Scanner(input);

                                                while (fileScanner.hasNext()) {

                                                                String line = fileScanner.nextLine();

                                                                queue.add(line);

                                                }

                                                /**

                                                * getting an iterator from the queue, and looping through contents

                                                */

                                                Iterator<String> iterator = queue.iterator();

                                                while (iterator.hasNext()) {

                                                                String text = iterator.next();

                                                                // reversing the text and pushing to the stack

                                                                String reversed = reverse(text, 0);

                                                                stack.push(reversed);

                                                }

                                                /**

                                                * defining a PrintWriter, writing the contents of stack into output

                                                * file as well as to the console

                                                */

                                                PrintWriter writer = new PrintWriter(output);

                                                while (!stack.empty()) {

                                                                String text = stack.pop();

                                                                System.out.println(text);

                                                                writer.println(text);

                                                }

                                                writer.close();// closing file writer

                                                System.out.println(" output has been saved to " + output

                                                                                + " file");

                                } catch (Exception e) {

                                                e.printStackTrace();

                                }

                }

}

// FileException.java

public class FileException extends Exception {

                public FileException(String message) {

                                super(message);// passing message to super class

                }

}

/*input.txt*/

seitiroirp rieht fo redro eht ni dessecorp era taht stcejbo erots - seueuQ ytiroirP

noihsaf tuo-tsrif ni-tsrif a ni dessecorp era taht stcejbo erots - seueuQ

noihsaf tuo-tsrif ,ni-tsal a ni dessecorp era taht stcejbo erots - skcatS

stnemele fo noitcelloc deredro na erots - stsiL

stnemele etacilpudnon fo puorg a erots – steS

/*output.txt*/

Sets - store a group of nonduplicate elements

Lists - store an ordered collection of elements

Queues - store objects that are processed in a first-in first-out fashion

Priority Queues - store objects that are processed in the order of their priorities

Stacks - store objects that are processed in a last-in, first-out fashion

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