Write a program that reverses a text file by using a stack. The user interface m
ID: 3791998 • Letter: W
Question
Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to a new text file. At first, only the Read button is enabled. After it is clicked, it is disabled and the Reverse button is enabled. After it is clicked, it is disabled and the Write button is enabled. After it is clicked, it is disabled. The name of the input text file is "input.txt". The input text file will contain no more than 100 lines of text. This fact is not needed by the program. It simply means that memory usage is not an issue. The name of the output text file is "output.txt". Notes: 1. The name of your main class should be ReverseFileViaStack. 2. Use the Java class library Stack class for your stack. 3. Use a border layout for your contents pane. 4. In the north region include the following title: Reverse a Text File via a Stack. 5. Use a panel in the center region to contain the 2 list boxes (side-by-side). 6. Place the 3 buttons in the south region. 7. A useful resource for this project is the week 5 video: Java GUI List Components
Explanation / Answer
The question is not clear because by reversal of text file, whether you mean reversal word-by-word or sencentence by sentence. Moreover, the GUI design comprises of multiple requirement as that of a complete project.
Hence, I give a command user based solution.
This solution reverses a text file using stack. It will clear the concept of working of stack. the program is as follows -
import java.util.Scanner;
import java.io.*;
class ReverseNumbers {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("numbers.txt");
Scanner Scan = new Scanner(file);
while(scanner.hasNextInt())
{
Integer next = scanner.nextInt();
numbers.push(next);
}
while(!numbers.empty())
{
Integer top = numbers.pop();
System.out.print(reverse + " ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.