This discussion is archived
6 Replies Latest reply: Jul 9, 2008 8:28 PM by 843785 RSS

Text file into arrayList

843785 Newbie
Currently Being Moderated
Can someone show me a good way to take a text file(Email.txt) with name email information set up as

Name
Name@mail.com

and feed this info into an arrayList ? The only thing that I can find in the books I have is ways to do it statically.
The program must read all the pairs, construct an Info object for each, and put all these objects in
a suitable list.

I need to be able to basically type in a Name and return an email address using a random text file as it is set up above.
I can feed the text file into my class using scanner or buffered reader but I dont understand how to put the text file data into an arrayList.



     ArrayList<Info> list =
new ArrayList<Info>();
  • 1. Re: Text file into arrayList
    843785 Newbie
    Currently Being Moderated
    This sounds kind of sort of like homework. Let's see a decent try by you towards a solution first, and then post what you have and your specific questions. I've found that usually the more thought and effort posters put into creating and solving their questions, the better their chances are of a volunteer here taking the time and effort to consider it and give a helpful answer.
  • 2. Re: Text file into arrayList
    843785 Newbie
    Currently Being Moderated
    import java.util.*;
    import java.io.*;

    public class Ex4
    {
         
         public BufferedReader Reader;
    public static void main(String[] args) throws Exception
    {
         
         // get the file name
    System.out.print("Enter name of your contact: ");
    Scanner keys = new Scanner( System.in );
    String word = keys.nextLine();



    BufferedReader inputFile =
    new BufferedReader(new FileReader("Email.txt"));

         // open the file for reading
         
         ArrayList<Info> lst =
    new ArrayList<Info>();

    Scanner input = new Scanner( inputFile );


    for ( int k=0; k<info.size(); k ++ )
    {
         info.add(input.next());
    }



    info.add(0, input.next());


    //Info info;
    boolean quit;
    boolean found;
    int k;
    String s;

    if( input.hasNext() )
    {
    System.out.println(String() );
    input.close();
    }

    }

    }
  • 3. Re: Text file into arrayList
    843785 Newbie
    Currently Being Moderated
    RayNoPlot wrote:
    public class Ex4
    {
    public BufferedReader Reader;
    public static void main(String[] args) throws Exception
    {
    Your code doesn't compile for me. What is the Info class? Where do you declare the "info" variable? Please try to post a compilable class for a better chance at getting help.

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview tab to make sure that your code is formatted correctly. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]
    or
    {&#99;ode}
      // your code block goes here.
      // note here that the tags are the same.
    {&#99;ode}
    good luck
  • 4. Re: Text file into arrayList
    843785 Newbie
    Currently Being Moderated
    Thanks for the tip.

    Ok this is a short version of the code and it has compiled for me. Just trying to understand how to enter a text input file and put it into an arrayList. The info class
    isnt a factor quite yet.

    import java.util.*;
    import java.io.*;
    
    public class Ex4
    {
         
         
        public static void main(String[] args) throws Exception
        { 
             
             // get the file name
          System.out.print("Enter name of your contact: ");
          Scanner keys = new Scanner( System.in );
          String word = keys.nextLine();
    
         
          
          BufferedReader inputFile = 
                new BufferedReader(new FileReader("Email.txt"));
          
             // open the file for reading
             
             ArrayList<String> info = 
                        new ArrayList<String>();
     
             // scan the file
           Scanner input = new Scanner( inputFile );
           
         
          for ( int k=0; k<info.size(); k ++ )
          {
               info.add(input.next()); 
          }
           
          
        
          info.add(0, input.next());  
          
          
          //Info info;
          boolean quit;
          boolean found;
          int k;
          String s;
          
          if( input.hasNext() )
          {
            System.out.println( input.next() );
            input.close(); 
          } 
        
       }
       
    }      
  • 5. Re: Text file into arrayList
    843785 Newbie
    Currently Being Moderated
    RayNoPlot wrote:
    Ok this is a short version of the code and it has compiled for me. Just trying to understand how to enter a text input file and put it into an arrayList. The info class
    isnt a factor quite yet.
    I'm guessing that each Info object will hold two Strings: name and Email address. If so, then it is a factor and is in fact one of the main factors of your program.

    Given what your text file looks like, I would use my scanner object to read two lines in at a time (but checking the Scanner for nextLine before reading in each line) and store each in a separate String. Then when both Strings have been obtained, create a new Info object (or whatever object you are using here) with the Strings and add this to the ArrayList.
  • 6. Re: Text file into arrayList
    843785 Newbie
    Currently Being Moderated
    Ok, I wasnt sure if there was a way to do that with the text file and arrayList or not.


    The approach your describing makes sense, Ill re work it that way, thanks for your help.