Results 1 to 6 of 6

Thread: Arraylists

  1. #1
    talia is offline Member
    Join Date
    Dec 2011
    Location
    London
    Posts
    11
    Rep Power
    0

    Default Arraylists

    Hi I need some help with arraylists, I am trying to load a text file into an array list with the following method:

    Java Code:
    public static ArrayList LoadFile(JTextArea src){
            String line = "";
            ArrayList data = new ArrayList();
            try{
                FileReader fr = new FileReader("Products.txt");
                BufferedReader br = new BufferedReader(fr);
                while ((line = br.readLine()) != null){
                    
                 src.append(line);
                    src.append(System.getProperty("line.separator"));
               } 
            }catch(FileNotFoundException fN) {
                    fN.printStackTrace();
                }
            catch(IOException e){
                System.out.println(e);
            }
            return data;
    I use netbeans, and have to make things work with a GUI. I can display the contents of this file in a JTextArea by pressing a Display button. What I would like to know is how to proceed from here. I want to be able to search the arraylist for a particular word when typed in the GUI and then display it. How is this possible. I have spent hours to figure this out so far and couldnt come up with a solution.
    Also the above arraylist is defined in a class, while the search and display is done in the MainFrame class.

    Hope some one can show me to the right direction.

    thanks
    Last edited by JosAH; 01-30-2012 at 04:57 PM. Reason: added [code] ... [/code] tags

  2. #2
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Arraylists

    Well splitting it up into lines and placing it into the textarea is all well and good for displaying it. For searching through it I would highly reccomend concatenating it all into a string, and using the String API to help you find the method for looking for certain patterns. For looking for certain patterns in strings, I would HIGHLY recommend teaching yourself regular expressions :)

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylists

    A List has an indexOf( ... ) method; it searches the list and if an element is found, its index is returned (otherwise -1 is returned). Read the API documentation for the List interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    christopherx is offline Member
    Join Date
    Oct 2011
    Posts
    92
    Rep Power
    0

    Default Re: Arraylists

    Quote Originally Posted by JosAH View Post
    A List has an indexOf( ... ) method; it searches the list and if an element is found, its index is returned (otherwise -1 is returned). Read the API documentation for the List interface.

    kind regards,

    Jos
    I think using the indexOf() method assumes that the pattern will match the whole line, which isn't always the case.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylists

    Quote Originally Posted by christopherx View Post
    I think using the indexOf() method assumes that the pattern will match the whole line, which isn't always the case.
    True, but then maybe a list of lines of text isn't the ideal data structure to solve the problem ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    talia is offline Member
    Join Date
    Dec 2011
    Location
    London
    Posts
    11
    Rep Power
    0

    Default Re: Arraylists

    Thanks, for your replies I will now have a look at all the recommended documentation and hopefully can solve this:)
    cheers....

Similar Threads

  1. help with ArrayLists !
    By bigbreez21 in forum New To Java
    Replies: 3
    Last Post: 11-20-2011, 08:23 PM
  2. ArrayLists
    By Freakzoyd in forum New To Java
    Replies: 4
    Last Post: 11-12-2010, 04:27 AM
  3. ArrayLists for BlueJ
    By heyit'skaye in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 04:15 AM
  4. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •