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

Lots of code, but i just need the code in red to be written in another way witho

ID: 3637753 • Letter: L

Question

Lots of code, but i just need the code in red to be written in another way without using the Search and replace API. Thats all please help!

import java.io.*;
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SecondGUI 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;
    SecondGUI copyOfMainWindow;

    public static void main (String [ ]args)
    {
      SecondGUI mainWindow = new SecondGUI ();
      mainWindow.setVisible (true);
     
    }

    public SecondGUI ()
    {
        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 ( ) + " ");

        }

        displayArea.setText (inputStream);
        inFile.close ();

    }


}

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 toSearch = searchField.getText ();
            String toReplace = replaceField.getText ();
           
            copyOfInputStream = copyOfInputStream.replaceAll(toSearch, toReplace);
           
            copyOfDisplayArea.setText (copyOfInputStream);
                
        }      
    }
}

Explanation / Answer

public void actionPerformed (ActionEvent ex) { String whichOne = ex.getActionCommand(); if (whichOne.equals ("clear")) { searchField.setText (""); replaceField.setText (""); } else if (whichOne.equals ("ok")) { String toSearch = searchField.getText (); String toReplace = replaceField.getText (); copyOfInputStream = copyOfInputStream.replaceAll(toSearch, toReplace); copyOfDisplayArea.setText (copyOfInputStream); } } }