Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Need help to fix my code

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help to fix my code

    please i need your help to fix my code...its actually running already but there's little problem..here's how it work first im adding data to a text file and now i need to delete it..my problem is when im going to press already the delete button all contents of the text field are being deleted.but i want to delete only the specific data that i typed in my textfield ..for example: i typed 123 on my text field and i press Add button then 123 will be added to the textfile("test.txt") then i want to delete it and now i will type 123 to the textfield and press delete button and now all data are deleted...that's my problem..pls i need your help hehe..any help will be appreciated
    example of my textfile("test.txt")
    hello //data i added
    hello //data i added
    hello //data i added
    123 //data i added

                word = ("");
                word = inputOutputField.getText().trim(); //variable name of my textfield
     
                try{
                    String lineToRemnove = null;
                    BufferedReader bf = new BufferedReader(new FileReader("test.txt"));
                    BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"));
     
                    while (( lineToRemnove = bf.readLine()) != null){
                        if(lineToRemnove.equals(word)){
                        JOptionPane.showMessageDialog(null,"Data deleted!");
                        writer.write(word);  
                    }
                    writer.close();  
                    bf.close();
                    } 
                }catch (IOException E) {
                    System.out.println("Error" + E);
                }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need help to fix my code

    I'm not sure I understand your problem.
    Is this what you are trying to do:
    There is a file with multiple lines.
    If the user enters some text and presses the Delete button, the program is supposed to update the file by deleting the line that contains the text that was entered.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help to fix my code

    Quote Originally Posted by Norm View Post
    Is this what you are trying to do:
    There is a file with multiple lines.
    If the user enters some text and presses the Delete button, the program is supposed to update the file by deleting the line that contains the text that was entered.
    yes that's it

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,321
    Thanks
    56
    Thanked 2,374 Times in 2,345 Posts

    Default Re: Need help to fix my code

    Can you explain what the current program does? Post the contents of the file before the program is executed and the contents of the file after the program is executed to show what the program does.

    You will need to post the full contents of the code for the program so we can test it if needed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help to fix my code

    here's my code sir
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
     
     
    public class Project9 extends JFrame implements ActionListener{
     
    	private JTextField tf;
    	private JButton addButton,searchButton,deleteButton;
        private String word;
     
    	public static void main(String[]args){
    	Project9 gui = new Project9();
        gui.setVisible(true);
    }
     
    public Project9()
    {
    	setTitle("File Manipulation");
    	setSize(950,150);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	Container cp = getContentPane();
    	cp.setLayout(new FlowLayout());
    	JButton jb1 = new JButton("Add");
    	jb1.setPreferredSize(new Dimension(150, 100));
    	jb1.setFont(new Font("Verdana", Font.BOLD, 30));
    	jb1.addActionListener(this);
    	JButton jb3 = new JButton("Delete");
    	jb3.addActionListener(this);
    	jb3.setPreferredSize(new Dimension(150, 100));
    	jb3.setFont(new Font("Verdana", Font.BOLD, 30));
    	tf = new JTextField(30);
    	add(tf);
    	add(jb1);
    	add(jb3);
    }
     
    public void actionPerformed(ActionEvent e)
    {
    	if (e.getActionCommand().equals("Add"))
    	{
            word = ("");
            word = tf.getText().trim();
            try{
              	if (word.length() > 0)
                {
                	BufferedWriter out = new BufferedWriter(new FileWriter(new File("test.txt"), true));
                    JOptionPane.showMessageDialog(null, "Data Added");
                    out.write(word);
                    out.newLine();
                    out.close();
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "No Data Added");
                }
            }catch (IOException E)
            {
                System.out.println("Error" + E);
            }
        }
         else if (e.getActionCommand().equals("Delete"))
            {
                word = ("");
                word = tf.getText().trim();
     
                try{
                    BufferedReader bf = new BufferedReader(new FileReader("test.txt"));
                    BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"));
                    String line = null;
     
                    while (( line = bf.readLine()) != null){
                        if(line.contains(word)){
                        JOptionPane.showMessageDialog(null,"Data deleted!");
                        writer.write(word);
                    }
                        writer.close();  
                        bf.close();
                    } 
                }catch (IOException E) {
                    System.out.println("Error" + E);
                }
     
            } 
    }
    }

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Need help to fix my code

    Man, correct your indentation. Start it from the EDT. Don't extend JFrame, you're just using it, so there's no need to extend it.
    BufferedReader bf = new BufferedReader(new FileReader("test.txt"));
    BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"));
    You're reading and writing to the same file. Write to a temp file, delete the original and rename the temp file to test.txt
    while (( line = bf.readLine()) != null){
                        if(line.contains(word)){
                        JOptionPane.showMessageDialog(null,"Data deleted!");
                        writer.write(word);
                    }
    so, if the word is found you write it to the file and display a message stating that it's deleted?!
    If your line contains the search word, you should not write it to the file.

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 08:19 AM
  2. How do i fix my code?
    By beebee007 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 14th, 2012, 11:49 AM
  3. Help me to fix this code
    By rcbandit2 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 31st, 2011, 02:30 PM
  4. What do i have to do to fix my code?
    By kram in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 07:13 AM
  5. How to fix this code?
    By ice in forum AWT / Java Swing
    Replies: 26
    Last Post: November 29th, 2010, 07:10 PM