hey everybody,
i have a text file containing some names

i place extract those names and i place it in an array an then i compare it with the input from JTextField i am able to compare them and find the values that match. what i want is
if the value is found i would want it do be deleted how could this be done ?

thank you very much any suggestions would be greatly appreciated

One way to do is to store all the file content somewhere (in the memory if the file is not too big), manipulate the content (delete certain text), and then rewrite the content to file as new (replace the whole file content with the content in the memory or wherever you store it in the first place).

well the file is not big at all just few lines, i actually read about this suggestion but im still not very sure how would this be done,
do you maybe have some good links where it shows how this could be done or any other examples.

thank you

OK, do you know how to read & write file in Java? There are many ways to do this. Also, do you know how to create panel containing a field (JTextField)? I assume that you know all of these already. If not, you should search on the Internet for 'Java read write file' for how to do the read/write file in Java. Also search for 'Java JtextField' for JTextField (may need to look for JButton and how to cooperate actionListener). The steps could be as followed:

1.Read file and save all string you want (names) in a name array when the application starts
2.Display a panel to accept an input
3.Accept the input and search in the name array
4.Delete the name from the array if found
4.1 One way to deal with this could be to write to the text file right away, or
4.2 Include a 'Save' button for user to force write to text file whenever the user clicks on it

You need to decide when you want to write to text file... It is not easy to find an example site that exactly matches to your assignment.

well i can easily perform all the steps up to step 4.
but im not sure i understand thereafter from step 4- do you mean that i should:
1:store the data in an array
2: display in JTextField (so the data is in memory)
3: set JTextField to editable(true)
4: manually delete the unwanted line(using backspace)
5: save the file again
thank you for you replay

Somewhat yes... You know how to do the search element in an array, right? What you need to do is to remove the element from the array if found. Then, the easiest way to deal with this is to save to file right away (not append, but overwrite). Then you shouldn't be worried about your file again.

thanks for you reply,
well i know how to read/write file and search for an element in an array. i would know how to overwrite the file, but im not sure how to actually remove a file from an array.
was i right by saying to use backspace to get rid of the element when the array is displayed in the JTextField?

Not really... Depends on how you build your array. Also, whatever inside the array is the string content you read from the file. If you use ArrayList, then you should be able to delete it using remove(obj). Also, you use setText("") to reset JTextField content.

ok i think i understand i just set the element that i found in array to empty value by using setText(""); and than i save it. the element would be replaced by an empty line.

You can also try this.

When its time to write your array back to your file, do a for loop and compare if the the array value is equal to the name you want to delete. If it is, then skip the write.

Eric Cute, good idea too if he needs 'redo' capability. :) However, rather than search in the existing text again, how about rewrite the whole file with correct array data without checking anything? :)

No, setText() is a method for JTextField only, not for an array you are working on.

ArrayList<String> arr = new ArrayList<String>();  // array list type String
// then populate the arr with content (or name) reading from file
// Supposed that you have populated, the arr could look like...
// ["name1", "name2", "name3", "name4 or not", ...]

// Then you are supposed to have JTextField created and added to a panel.
// Need to set whatever property to the JTextField too!
JTextField jf = new JTextField();  // sample only
// Don't forget to attach an action listener to the JTextField.
// The method inside the action listener could be like...
arr.remove(jf.getText());  // it will remove the name if found; otherwise it automatically ignores this command

// Now your array might have been modified (deleted the selected name).
// You could write to file right after you reset your JTextField
// for simplicity.

Hope this would clarify a bit more.

ok now i have clear understanding how this would be done thanks a lot, you have been very helpful
i appreciated all of this help
thank you

It seems inefficient to write 99 lines to a file each time you delete the 100th. Is that the only way to go?

But then again, by the time you get to complex applications, you'll be using databases instead of .txt files.

As I said, for the simplicity, not efficiency.

hi friends
is there any other approach to Solve this probelm.
why because every time here u are createing a temp file and writing into that file it takes to much time to do this process.that is the disadvantage of this process

please replay me

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.