Results 1 to 4 of 4
  1. #1
    MiitchKay is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Removing a word from a txt file

    Okay so I'm making a program in which I need to remove one word from a .txt file by using a button and text box (Swing). But the problem is I have no idea how to remove a single line/word from the .txt file that my program is using. Basic idea of twhat im doing:

    [Add name] (Mitchell) [Remove Name] (Kyle) <----- Name to remove

    List of names: <------Listname.txt being printed in a textarea
    Mitchell
    Kyle <--------- Name to remove
    George
    Fred



    I could really use some help on this, it would be greatly appritiated :)

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by MiitchKay View Post
    Okay so I'm making a program....
    What have you made so far?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    MiitchKay is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Ill post it once I get to my next class :P in about 10 minutes

  4. #4
    MiitchKay is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class TabbedPaneExample extends JFrame implements ActionListener
    {
    
    
        //Making all the different GUI/Swing Components
        private JTabbedPane tabbedPane;
    
        private JPanel panel1;
        private JPanel BanPanel1;
        private JPanel BanPanel2;
        private JPanel BanPanel3;
    
        private JPanel panel2;
        private JPanel panel3;
        private JPanel panel4;
    
    
        private JButton AddBan;
        private JButton RemoveBan;
    
        private JTextField BanAddName;
        private JTextField BanRemoveName;
    
        private JTextArea BanList;
        
        private JScrollPane BanListScroll;
    
    
        String Banaddstring, Banremovestring;
    
        public TabbedPaneExample ()
        {
    
    	setTitle ("Admin Tools");
    	setSize (425, 300);
    
    
    	JPanel topPanel = new JPanel ();
    	topPanel.setLayout (new BorderLayout ());
    	getContentPane ().add (topPanel);
    
    	// Create the tab pages
    	createPage1 ();
    	createPage2 ();
    	createPage3 ();
    	createPage4 ();
    
    
    
    	// Create a tabbed pane
    	tabbedPane = new JTabbedPane ();
    	tabbedPane.addTab ("Bans", panel1);
    	tabbedPane.addTab ("WhiteList", panel2);
    	tabbedPane.addTab ("Ops", panel3);
    	tabbedPane.addTab ("Server Props", panel4);
    	topPanel.add (tabbedPane, BorderLayout.CENTER);
    
    
    
        }
    
    
        public void createPage1 ()
        {
    	panel1 = new JPanel ();
    	BanPanel1 = new JPanel ();
    	BanPanel2 = new JPanel ();
    	BanPanel3 = new JPanel ();
    	BanList = new JTextArea (10, 30);
    	BanListScroll = new JScrollPane(BanList);
    	panel1.setLayout (new FlowLayout ());
    	BanPanel1.setLayout (new FlowLayout ());
    	BanPanel2.setLayout (new FlowLayout ());
    	BanPanel3.setLayout (new FlowLayout ());
    
    	BanList.setEditable (false);
    
    	BanAddName = new JTextField (7);
    	BanRemoveName = new JTextField (7);
    
    	AddBan = new JButton ("Add Ban");
    	RemoveBan = new JButton ("Remove Ban");
    
    	AddBan.setActionCommand ("addban");    // set the  command
    	RemoveBan.setActionCommand ("removeban");
    	
    	panel1.add (BanPanel1);
    	panel1.add (BanPanel2);
    	panel1.add (BanPanel3);
    	BanPanel1.add (AddBan);
    	BanPanel1.add (BanAddName);
    	BanPanel2.add (RemoveBan);
    	BanPanel2.add (BanRemoveName);
    	BanPanel3.add (BanListScroll);
    
    	AddBan.addActionListener (this);
    	RemoveBan.addActionListener (this);
    	
    	printbannedplayers ();
    
        }
    
    
        public void createPage2 ()
        {
    	panel2 = new JPanel ();
    	panel2.setLayout (null);
        }
    
    
        public void createPage3 ()
        {
    	panel3 = new JPanel ();
    	panel3.setLayout (null);
        }
    
    
        public void createPage4 ()
        {
    	panel4 = new JPanel ();
    	panel4.setLayout (null);
        }
    
    
        public void printbannedplayers ()
        {
    	try
    	{
    	    BufferedReader input = new BufferedReader (new FileReader ("banned-players.txt"));
    	    try
    	    {
    		String line = null;
    
    		while ((line = input.readLine ()) != null)
    		{
    		    BanList.append (line + "\n");
    		}
    	    }
    	    finally
    	    {
    		input.close ();
    	    }
    	}
    	catch (IOException ex)
    	{
    	    ex.printStackTrace ();
    	}
        }
    
    
        public void actionPerformed (ActionEvent evt)
        {
    
    	String EnteredBan, EnteredRemoveBan;
    	String banName = ("banned-players.txt");
    
    
    	if (evt.getActionCommand ().equals ("addban")) //What happens when you press Add Ban
    	{
    	    EnteredBan = BanAddName.getText ();
    
    	    try
    	    {
    		BufferedWriter out = new BufferedWriter (
    			new FileWriter (banName, true));
    		out.write (EnteredBan);
    		out.newLine ();
    		out.close ();
    
    	    }
    	    catch (IOException iox)
    	    {
    		System.out.println ("Problem writing " + banName);
    	    }
    	   
    	    BanList.setText(""); 
    	    printbannedplayers ();
    
    
    	}
    
    
    	else if (evt.getActionCommand ().equals ("removeban"))
    	{
    	    EnteredRemoveBan = BanRemoveName.getText ();
    	}
    	repaint ();
        }
    
    
        // Main method to get things started
        public static void main (String args[])
        {
    	// Create an instance of the test application
    	TabbedPaneExample mainFrame = new TabbedPaneExample ();
    
    	WindowQuitter wquit = new WindowQuitter ();
    	mainFrame.addWindowListener (wquit);
    
    	mainFrame.setVisible (true);
    
    
        }
    }
    
    class WindowQuitter extends WindowAdapter
    {
        public void windowClosing (WindowEvent e)
        {
    	System.exit (0);
        }
    }
    There is the whole program so far. It's far from complete but it's getting there.

Similar Threads

  1. removing the last blank line from txt file
    By heartysnowy in forum New To Java
    Replies: 8
    Last Post: 12-06-2013, 01:09 PM
  2. Jfilechooser removing all file types
    By counterfox in forum New To Java
    Replies: 2
    Last Post: 02-15-2011, 10:55 AM
  3. Count same word from many file in directory
    By cassiests in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 09:21 AM
  4. printin word file
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 07-12-2009, 03:26 PM
  5. Replies: 7
    Last Post: 11-19-2008, 02:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •