This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
i got a gui were you can search for people in a txt file and wen it finds a match it prints the match in a jtextArea.
whit the following code i enter an ID in a textfied and wen i press a button called Search it print me the ID, but i want also to print me the 2 elements before the ID too.
oh i forgot i pass my txt into an arraylist .
I found one of your previous posts, so I suppose you use the same txt you used over there:
Alfredo Garcia 34353454
Richard Blemen 23525342
Mick Jagger 24523523
Robert Pattinson 65434518
Hayden Pannetiere 36486712
Alice Eve 97246590
Hugo Beaven 28288282
Beavis Acdc 54545445
Buthead Acdc 12121221
Mathias Oviedo 78787887
Marcos Viera 29292992
Wesker Almond 33333333
Trinity Gogo 22222222
Tia Carrera 44444444
Kristen Stweart 66666666
Charlize Theron 77777777
Denzel Washington 99999999
Angus Young 23312323
Malcom Young 23311332
Jhon Lennon 57575775
Jason Statam 83838383
Mario Bros 91291291
Luigi Bros 48297538
Amy Adams 98239832
Isla Fisher 20202020
So, if you want to print first name, last name and id from this text file, there is no need to read it into ArrayList. Just read the file line by line, and when current line (String) contains the id you are looking for, do whatever you want with it.
First, you are comparing strings with == which should never be done. If you want to check them for equality, use equals method.
On the other hand, you will never get a line from file that is equal to id you enter in your text field. You should check if the line read from file contains the id you provided, hence:
Use the if statement I provided you in the previous post.
I told you about equals just to know it for the future programs you write, not to use == operator. You don't need it for your problem, just use contains.
Marcelo Bordolli wrote:Question now if i want to remove that line that i search from the .txt file?
Well, that's the place where I would use ArrayList to keep all the lines read. Once you find the line you are looking for, remove it from the list, and then write the contents of the list to the same or another file.
EDIT: Perhaps better solution would be to use existing code; if the if statement you have is evaluated to true you don't add that line to your list; otherwise, add line to list. Once you finish reading a file, write all the lines from list into the same or another file.
ok nice!! but if i write all the lines into a file whit the same name it wont collide whit the exiting file whit that name, i mean it just override the older file and create this new one whit the same name?.
Marcelo Bordolli wrote:ok nice!! but if i write all the lines into a file whit the same name it wont collide whit the exiting file whit that name, i mean it just override the older file and create this new one whit the same name?.
FileWriter class contains constructor where you indicate if you want to append to or overwrite the existing file. Check API for more details.
for erasing a line i try this code but instead of erasing a line and create a new txt file whit the same name and whit the rest of the lines it just erase everything and saves a funky line like this java.io.bufferreader@213dcn
Ok, let's go one thing at a time (though I think you should have created a new topic for this one).
First of all, your logic is not good now.
In your while loop, each iteration creates a new ArrayList, so actually you can only have in it an item from last iteration (that's why you get only one line in your output file). To resolve this, you need to instantiate your ArrayList before entering while loop.
Next, in while loop, you are reading strings (line by line), but at line 8 of your code you add lee variable to your ArrayList; lee variable in this case is an instance of BufferedReader, and what you are writing to your output file is actually an address of that instance in memory (hence the result you get).
Furthermore, you are also writing to output file in each iteration of your while loop, so only the last iteration is actually counted - and the result you get is the address of above mentioned BufferedReader.
These are some directions you should follow now:
Initialize ArrayList before you enter while loop; I would prefer ArrayList<String> over generic one you are using
Enter while loop; if the current line read contains id you are looking for (logic already implemented as a solution for your original problem), continue to next line; otherwise, add line to list. This way the line containing id you provided will be omitted from the list, hence it will be omitted from the output file
After the end of while block, do the saving as you did in your code
Also, omit those "\n"s you are adding. out.println() adds new line, so no need to create extra blank space between lines.
Glad it's working!
Yet, there are points that you didn't follow. There really is no need to add then remove string from ArrayList; simply, don't add it. Also, what you are actually doing in your code is that in each iteration in while loop you are writing contents of your ArrayList to the destination file.
Here is what should be done (approximately):