wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes issue in a gui Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "issue in a gui" Watch "issue in a gui" New topic
Author

issue in a gui

Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

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



I know is only java
BUT I LIKE IT!!!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

What is the format of your txt file? And what are the 2 elements you want to print too?


The quieter you are, the more you are able to hear.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Sep 03, 2012
Posts: 46

yes kemal that the txt file, ok ill try to do what you say.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Sep 03, 2012
Posts: 46

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

Joined: Sep 03, 2012
Posts: 46

Tex is initialized String static Tex="";
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Sep 03, 2012
Posts: 46

I Still can see the issue , now whit this it always print me the else clause


Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Sep 03, 2012
Posts: 46

Still Always prints the else clause:

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Sep 03, 2012
Posts: 46

Fixed!!! Thank you as always Kemal!! i Will have to put your name on the design team

Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

Question now if i want to remove that line that i search from the .txt file?
Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

i mean remove it from the file , like erase it in the file
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Sep 03, 2012
Posts: 46

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

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Sep 03, 2012
Posts: 46

Thanks Kemal, i will do that
Marcelo Bordolli
Ranch Hand

Joined: Sep 03, 2012
Posts: 46

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

Joined: Sep 03, 2012
Posts: 46

it saves that funky line on the new whit same name than before file, were are the rest of the lines?¿?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

    Joined: Sep 03, 2012
    Posts: 46

    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

    Joined: Jun 19, 2010
    Posts: 825
        
        5

    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

    Joined: Sep 03, 2012
    Posts: 46

    Oh yes, is better that way for sure!! im going to implement that!! Thanks as always!!
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: issue in a gui
     
    Similar Threads
    Multiline regex on InputStream
    file search using scanner
    Updating values in a jTable
    Update Database
    How to add elements from a file into an Array List