• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

issue in a gui

 
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .

that ny isuue

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the format of your txt file? And what are the 2 elements you want to print too?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes kemal that the txt file, ok ill try to do what you say.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would also suggest to use BufferedReader for reading a file.

If you have any other issues, feel free to ask!
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did that but it just dont find a match wen i pit in the textfield an ID and press Search.

 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tex is initialized String static Tex="";
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Still can see the issue , now whit this it always print me the else clause


 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still Always prints the else clause:

 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are reading just one line of file. Your original code contained while loop that read each line of the file. Add it again and inside of the loop put the conditions you already have.

 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed!!! Thank you as always Kemal!! i Will have to put your name on the design team

 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question now if i want to remove that line that i search from the .txt file?
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i mean remove it from the file , like erase it in the file
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kemal, i will do that
 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marcelo Bordolli
Ranch Hand
Posts: 46
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it saves that funky line on the new whit same name than before file, were are the rest of the lines?¿?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Marcelo Bordolli
    Ranch Hand
    Posts: 46
    Android Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Fixed now it erase the name last name and Id and print the rest in a perfect way!!

    Thanks Kemal the guru on swing!!

    Final code look like this:

     
    Kemal Sokolovic
    Bartender
    Posts: 825
    5
    Python Ruby Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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):
     
    Marcelo Bordolli
    Ranch Hand
    Posts: 46
    Android Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh yes, is better that way for sure!! im going to implement that!! Thanks as always!!
     
    Squanch that. And squanch this tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic