So my assignment was to write a search and replace GUI without using the replaceAll method. My problem is in my search and replace code I am able to display the new string in a printout but I'm needing some help on how to display the new string in the original window when you open up the txt file. Can someone help me out with this please?
import java.io.*; import java.util.Scanner; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Gui extends JFrame implements ActionListener { public JMenuBar mainMenu; public JMenu fileMenu; public JMenuItem searchMenuItem; public JMenuItem openItem; public JMenuItem saveItem; public JMenuItem exitItem; public JMenuItem miscItem; public JMenuItem aItem; public JMenuItem bItem; public JMenuItem cItem; public Container contentPane; String inputStream; JTextArea displayArea; JScrollPane scrollPane; Gui copyOfMainWindow; public static void main (String [ ]args) { Gui mainWindow = new Gui (); mainwindow.setVisible (true); } public Gui () { setSize (400,600); contentPane = getContentPane ( ); setDefaultCloseOperation (EXIT_ON_CLOSE); mainMenu = new JMenuBar (); fileMenu = new JMenu ("file"); searchMenuItem = new JMenuItem ("search"); searchMenuItem.addActionListener(this); mainMenu.add (fileMenu); openItem = new JMenuItem ("open"); openItem.addActionListener(this); saveItem = new JMenuItem ("save"); exitItem = new JMenuItem ("exit"); miscItem = new JMenu ("misc"); aItem = new JMenuItem ("a"); bItem = new JMenuItem ("b"); cItem = new JMenuItem ("c"); miscItem.add (aItem); miscItem.add (bItem); miscItem.add (cItem); fileMenu.add (openItem); fileMenu.add (saveItem); fileMenu.add (searchMenuItem); fileMenu.add (exitItem); fileMenu.add (miscItem); setJMenuBar (mainMenu); displayArea = new JTextArea ("",40,50); scrollPane = new JScrollPane (displayArea); contentPane.add (scrollPane); //default: FlowLayout } public void actionPerformed (ActionEvent ex) { String whichOne = ex.getActionCommand(); if (whichOne.equals ("open")) openAndDisplay (); else if (whichOne.equals ("search")) searchAndReplace (); } public void searchAndReplace () { DialogBox a = new DialogBox (inputStream, displayArea); } public void openAndDisplay () { File aFile = null; JFileChooser fileChooser = new JFileChooser ( ); if (fileChooser.showOpenDialog (null) == JFileChooser.APPROVE_OPTION) aFile = fileChooser.getSelectedFile ( ); Scanner inFile = null; boolean fileOK = false; try { inFile = new Scanner (aFile); fileOK = true; } catch (FileNotFoundException ex) { inputStream = ""; } if (fileOK) { while (inFile.hasNext ( )) inputStream += (inFile.nextLine ( ) + "\n"); } displayArea.setText (inputStream); inFile.close (); } } Here is where the code is for the search and replace import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DialogBox extends JFrame implements ActionListener { private JTextField searchField; private JTextField replaceField; private JLabel searchLabel; private JLabel replaceLabel; private Container contentPane; private JMenuBar mainMenu; private JMenuItem okMenuItem; private JMenuItem clearMenuItem; private JMenu actionMenu; private String copyOfInputStream; private JTextArea copyOfDisplayArea; public DialogBox (String inputStream, JTextArea displayArea) { setSize (200,50); setDefaultCloseOperation (DISPOSE_ON_CLOSE); contentPane = getContentPane (); mainMenu = new JMenuBar (); actionMenu = new JMenu ("action"); mainMenu.add (actionMenu); okMenuItem = new JMenuItem ("ok"); clearMenuItem = new JMenuItem ("clear"); okMenuItem.addActionListener(this); clearMenuItem.addActionListener(this); actionMenu.add (okMenuItem); actionMenu.add (clearMenuItem); setJMenuBar (mainMenu); searchLabel = new JLabel ("search for:"); replaceLabel = new JLabel ("replace with:"); searchField = new JTextField (20); replaceField = new JTextField (20); searchField.setText (""); replaceField.setText (""); contentPane.setLayout (new FlowLayout ()); contentPane.add (searchLabel); contentPane.add (searchField); contentPane.add (replaceLabel); contentPane.add (replaceField); copyOfInputStream = inputStream; copyOfDisplayArea = displayArea; this.setVisible (true); } public void actionPerformed (ActionEvent ex) { String whichOne = ex.getActionCommand(); if (whichOne.equals ("clear")) { searchField.setText (""); replaceField.setText (""); } else if (whichOne.equals ("ok")) { String original = copyOfInputStream; String toSearch = searchField.getText (); String toReplace = replaceField.getText (); int startPt = 0; int floatPt = 0; do { if (floatPt == original.length ( )) System.exit (0); int searchPt = 0; do { if (toSearch.charAt (searchPt) == original.charAt (floatPt)) break; else { floatPt++; if (floatPt == original.length ( )) System.exit (0); } } while (true); startPt = floatPt; searchPt++; floatPt++; boolean aMatchFound = true; while (searchPt < toSearch.length ( )) { if (toSearch.charAt (searchPt) == original.charAt (floatPt)) { searchPt++; floatPt++; } else { aMatchFound = false; break; } } if (!aMatchFound) { continue; } else { String first = original.substring (0,startPt); String second = original.substring (floatPt); original = first + toReplace + second; floatPt = startPt + toReplace.length ( ); System.out.println ("a match found"); System.out.println ("\nnew string = " + original + "\n"); System.out.println ("new floatPt = " + floatPt); } } while (true); } } }