I'm trying to make a program that removes all occurences of a specified string from a text file. I'm having trouble with tying in everything to make it work, so that the user is prompted to enter a string.

This is what I've got:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class RemoverFrame extends JFrame
{
    private JLabel prompt; // label to prompt user to enter 
    private JLabel display; // label to display 
    private JTextField textEntered; // textfield to enter text

    // constructor sets up GUI
    public RemoverFrame()
    {
        super("Text Removal");

    prompt = new JLabel("Enter text to remove:");
    textEntered = new JTextField(10); // textfield for text entered

    // register anonymous action listener
    textEntered.addActionListener(
        new ActionListener() // anonymous inner class
        {
            public void actionPerformed(ActionEvent e)
            {
                        int texted = Integer.parseInt(textEntered.getText());
            }
        } // end anonymous inner class
    ); // end call to addActionListener

    display = new JLabel("Text removed:");

    add(prompt, BorderLayout.NORTH); // north region
    add(textEntered, BorderLayout.CENTER); // center region
    add(display, BorderLayout.SOUTH); // south region
    } // end RemoverFrame constructor
} // end class RemoverFrame

Any help would be great!

import java.io.*;
import java.util.*;

public class MyRemover {
    private static String textEntered;
    public static void main(String[] args) throws Exception {
    // Check command line parameter usage
    /* if (args.length != 2) {
        System.out.println(
            "Usage: java MyRemover stringToBeRemoved sourceFile");
        System.exit(1);
    } */

    // Check if the file exists
        String fileName = "myFile.txt";
    File sourceFile = new File(fileName);
    if (!sourceFile.exists()) {
        System.out.println("Source file " + fileName + " not exist");
        System.exit(2);
    }

    // Read text from the file and save it in a string builder
    Scanner input = new Scanner(sourceFile);
    StringBuilder sb = new StringBuilder();

    String lineSeparator = System.getProperty("line.separator");
    boolean firstLine =  true;
        String toBeRemove = textEntered;
    while (input.hasNext()) {
        String s1 = input.nextLine();
        String s2 = s1.replaceAll(toBeRemove, "");
        if (firstLine) {
            sb.append(s2);
            firstLine = false;
        }
        else
            sb.append(lineSeparator + s2);
    }

    input.close();

    // Write back to the file
    PrintWriter output = new PrintWriter(sourceFile);
    output.println(sb.toString());
    output.close();
    }
}





import javax.swing.JFrame;

public class Remover
{
    public static void main(String[] args)
    {
        RemoverFrame removerFrame = new RemoverFrame();
        removerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        removerFrame.setSize(275, 100); // set frame size
        removerFrame.setVisible(true); // display frame
    }
} // end class Remover 

Recommended Answers

All 3 Replies

what specifically are you having difficulties with?

Having the input from the user, say they enter the word "test", and have it be removed from a specified file.

You get the input from the user, but you parse it as an int. Why is that?

You have code (lines 14 - 46) that remove a given string from a given file. You can re-package that code as a method, passing the string and the file name as parameters. You can test that using your old main method, or call it from the GUI when you have the user's input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.