View the step-by-step solution to:

Course Project Guidance File input/output (You should complete this part during week 3.) Make sure you use relative (not absolute) paths for the...

This question was created from Course Project Guidance.docx https://www.coursehero.com/file/31017361/Course-Project-Guidancedocx/

31017361-175996.jpeg

CAN SOMEONE REVIEW MY CODE? I cannot get my program to reverse a text file by using a stack. Here is my code: import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; import javax.swing.border.*; import java.util.*; //Suggested class statement: public class ReverseFileViaStack extends JFrame implements ActionListener { //Container: private final Container contents; //JPanel: private final JPanel listPanel,buttonPanel; //Lists: private final JList<String> list1; private final JList<String> list2; //Label Text: private final JLabel labelText; //Text Field: private final JTextField text; //Button Labels: private final JButton Read,Reverse,Write; //Fonts: private final Font fontBold = new Font(Font.DIALOG, Font.BOLD, 14); private final Font fontPlain = new Font(Font.DIALOG, Font.PLAIN, 14); //List models: private DefaultListModel<String> listModel1 = new DefaultListModel<>(); private final DefaultListModel<String> listModel2 = new DefaultListModel<>(); // Stack (from the Java Collections Framework): private final Stack<String> stack = new Stack<>(); //Constructor. public ReverseFileViaStack() { //Title: super("Reverse A File Via Stack"); // Get container for components. // Contents is the main container. contents = new JPanel(); setContentPane(contents); //Layout Manager: contents.setLayout(new BorderLayout()); //instantiate GUI components and other variables labelText = new JLabel("Reverse Text File Via Stack", SwingConstants.CENTER); Read = new JButton("Read"); Reverse = new JButton("Reverse"); Write = new JButton("Write"); // Panels listPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); text=new JTextField(60); // the lists //setup the entire UI. JLabel list1Text; list1Text = new JLabel("Original order:"); list1Text.setAlignmentX(LEFT_ALIGNMENT); listModel1 = new DefaultListModel(); list1 = new JList(listModel1); list1.setAlignmentX(LEFT_ALIGNMENT); JLabel list2Text; list2Text = new JLabel("Reversed order:"); list2Text.setAlignmentX(LEFT_ALIGNMENT); list2 = new JList<>(listModel2); list2.setAlignmentX(LEFT_ALIGNMENT); // scrolling lists JScrollPane scrollList1 = new JScrollPane(list1); scrollList1.setAlignmentX(LEFT_ALIGNMENT); scrollList1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); JScrollPane scrollList2 = new JScrollPane(list2); scrollList2.setAlignmentX(LEFT_ALIGNMENT); scrollList2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); //add the components to the ccntent pane buttonPanel.add(Read); buttonPanel.add(Reverse); buttonPanel.add(Write); listPanel.add(list1Text); listPanel.add(list2Text); listPanel.add(scrollList1); listPanel.add(scrollList2); contents.add(buttonPanel,BorderLayout.SOUTH); contents.add(listPanel,FlowLayout.CENTER); contents.add(labelText,BorderLayout.NORTH); setSize(300,200); setVisible(true); } private abstract class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e, Object btnRead, Object btnReverse, Object btnWrite) { Object source = e.getSource(); if (source == btnRead) { readFile(); Read.setEnabled(true); Reverse.setEnabled(false ); Write.setEnabled( false ); return; } if (source == btnReverse) { reverseFile(); Reverse.setEnabled( true ); Read.setEnabled( false ); Write.setEnabled( false ); return; } if (source == btnWrite) { writeFile(); Write.setEnabled( true ); Read.setEnabled( false ); Reverse.setEnabled(false); } } } // Reads the input text file line-by-line. // Adds each line it gets to the left list box. // To add each item (line) to the left list box, use its list model // addElement function. private void readFile() { String line; try { FileReader fileReader = new FileReader("input.txt"); BufferedReader bufferedReader = new BufferedReader(fileReader); while((line = bufferedReader.readLine())!= null) { listModel1.addElement(line); bufferedReader.close(); } } catch(IOException ex) { System.out.println("Error reading file"); } } // Pushes each item from the left list onto the stack. // This uses the stack's push function. // elementAt function (with a loop index). // Then pops the items from the stack one-by-one and adds each of them // to the right list. // This uses the stack's pop function. // To add each item (line) to the right list box, use its list model // To read each item in the left list box, use its list model // addElement function. // This results in the right list being in reverse order of the left. private void reverseFile() { if (!stack.isEmpty()){ stack.empty(); } for (int i = 0; i <listModel1.size(); i++) { stack.push(listModel1.getElementAt(i)); } if (!stack.isEmpty()){ for(int i = listModel1.size() - 1; i >= 0; i--) { listModel2.addElement(stack.get(i)); } btnReverse.setEnabled(false); btnWrite.setEnabled(true); } } // See the video. private void setFonts() { UIManager.put("Button.font", fontBold); UIManager.put("ComboBox.font", fontBold); UIManager.put("Label.font", fontBold); UIManager.put("List.font", fontPlain); } // See the video. private void setSpecificSize(JComponent component, Dimension dimension) { component.setMinimumSize(dimension); component.setPreferredSize(dimension); component.setMaximumSize(dimension); } // Reads through the right list item-by-item. // Uses a PrintWriter to write these items to the output file. // Be sure to close the PrintWriter. private void writeFile() { for (int i = 0; i < listModel2.size(); i++) { File file; file = new File("output.txt"); try (PrintWriter printWriter = new PrintWriter(file)) { printWriter.println(listModel2); } catch (FileNotFoundException e) { } } } public static void main(String[] args) { ReverseFileViaStack gui = new ReverseFileViaStack(); //gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { } } Please review document attached to this question.

31017361-175996.jpeg

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question