Output values from Arrays of Objects

Ok..

I have Email objects,
          Email[] arrays of Email objects.

What I need to do is add all the Email objects in an ArrayList and convert the ArrayList to type Email[]
I need to do that for now for a later condition.

Now I want to get back the Email objects and test by printing out their values.

I tried the following but not working?
Any clue? (The SOP did work though)

Email lem;
Email[] lEmailRecords;

ArrayList lDataArrayList;

String lDataLine = null;
BufferedReader mDataFileReader;
      
      public void read(){
            try{
             mDataFileReader =  new BufferedReader(new FileReader("C:/Projects/Feedback2.txt"));
             lDataArrayList = new ArrayList();
             while((lDataLine = mDataFileReader.readLine())!= null){
                  
                   lem = new Email(lDataLine);
                  
                   System.out.println(" " + "In read()" +  lem); // this worked

                   lDataArrayList.add(lem);
                  
                    lEmailRecords = new Email[lDataArrayList.size()];
                    lEmailRecords = ( Email[] ) lDataArrayList.toArray(lEmailRecords);
                                }
                               catch(Exception e){}
                     }

public void print(){
            for (int i = 0; i < lEmailRecords.length; i++){
                  System.out.println(" " + lEmailRecords[i].getEmailAddress()); //this did not work ???
                  System.out.println(" " + "In print()");
                  
            
            }
            
            
      }

What's wrong???

Thanks
_Esam.
_EsamAsked:
Who is Participating?
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
I'd told you in your previous question to keep it outside the loop :)
0
 
CEHJConnect With a Mentor Commented:
>>
 lEmailRecords = new Email[lDataArrayList.size()];
                 lEmailRecords = ( Email[] ) lDataArrayList.toArray(lEmailRecords);
>>

should be *outside* the loop
0
 
_EsamAuthor Commented:
This>>

 for (int i = 0; i < lEmailRecords.length; i++){
               System.out.println(" " + lEmailRecords[i].getEmailAddress()); //this did not work ???
               System.out.println(" " + "In print()");
               
         
          }

Did not print in the print() method...
But if I move it in the read() , it does print...

Is there a scope problem here...Could you explain..??

Thanks.
_Esam
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
_EsamAuthor Commented:
Anyway,

What I need to do is set the objects to a method of a class that takes an array of objects:
Like so:

public class EmailMessageContainer{

publice void setMessages(Object[] mEmailObjects){}

publice Object[] getMessages(){}


}

Now, I want set the objects as:

EmailMessageContainer emc = new EmailMessageContainer();

emc.setMessages(lEmailRecords)

Now, on the other side,

I want to get the objects and cast it to proper type as:

messages = (Email[])pDataContent.getMessages()  // this should return the Email objects

Hope I am clear...

I want to make sure what's wrong in my first post...so that I can move along with the second class...

Thanks

_Esam

0
 
CEHJCommented:
When and where are you calling print?
0
 
_EsamAuthor Commented:
In the main() --

public static void main(String[] args) {
            
            // TODO Auto-generated method stub
            EmailMain lEmainMain = new EmailMain();
            System.out.println(" " + "What's going on");
            lEmainMain.read();
            lEmainMain.print();

      }

??

_Esam
0
 
CEHJCommented:
>>lem = new Email(lDataLine);

may not be initializing the members correctly
0
 
_EsamAuthor Commented:
>>lem = new Email(lDataLine);

may not be initializing the members correctly

Oh...No...


This is what I have:


public Email(){
      initializeObject();
}

public Email(String pDataLine)
{
    this();
      buildObject(pDataLine);
      
}  



/**
*    <p>.</p>
*/
private void initializeObject()
{

    mEmailAddress        = null;
    mRecordTypeCode = null;
    mSubCode                    = null;
    mEmailAddressNew          = null;
    mDateTime             = null;

}  // method initializeObject
 
private void buildObject(String pDataLine){
      
              pDataLine = pDataLine.trim();
              mEmailAddress =
                    pDataLine.substring
                    (fEmailAddress_Start,
                     fEmailAddress_End).trim();
            mRecordTypeCode =
                  pDataLine.substring
                    (fRecordTypeCode_Start,
                     fRecordTypeCode_End).trim();
            mSubCode =
                  pDataLine.substring
                    (fSubCode_Start,
                     fSubCode_End).trim();
            mEmailAddressNew =
                  pDataLine.substring
                    (fEmailAddressNew_Start,
                     fEmailAddressNew_End).trim();
            mDateTime =
                  pDataLine.substring
                    (fDateTime_Start,
                     fDateTime_End).trim();
            
              
}

Thanks..
_Esam
0
 
CEHJCommented:
How is your getEmailAddress() method defined?
0
 
_EsamAuthor Commented:
Like so:

public String getEmailAddress()
{
    return mEmailAddress;
}

In the Email class.

_Esam
0
 
_EsamAuthor Commented:
Well CEHJ,

I do get the email address with getEmailAddress if I try it in the read()  method?

Doesn't work in the print method???

_Esam
0
 
_EsamAuthor Commented:
Well, I will check the email in Half an hour again..
Please do drop a line...

Thanks..
_Esam
0
 
_EsamAuthor Commented:
I guess CEHJ is gone for the day :)
I will check tomorrow then...

_Esam
0
 
_EsamAuthor Commented:
Hi CEHJ,

I guess it was a scope problem...
Once I moved the ArrayList creation at the top, it did work...

Here is what I did:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;

import com.aa.ed.cecevfeed.data.Email;
// com.aa.ed.cecevfeed.data.EmailBehavior;

public class EmailMain {
      Email lem;
      ArrayList lDataArrayList = new ArrayList();;
      BufferedReader mDataFileReader;
      String lDataLine = null;
      Email[] lEmailRecords;
      
      public void read(){
            try{
             mDataFileReader =
             new BufferedReader(new FileReader("C:/Projects/Feedback.txt"));
             while((lDataLine = mDataFileReader.readLine()) != null){
                   lem = new Email(lDataLine);
                   lDataArrayList.add(lem);
                   lEmailRecords = new Email[lDataArrayList.size()];
                   lEmailRecords = (Email[]) lDataArrayList.toArray(lEmailRecords);
                   System.out.println(" " + lem);
                  
             };
            
            
            }
            catch (Exception e){
                  
            }
      }

      
      public void print(){
            for (int i = 0; i < lEmailRecords.length; i++){
                  System.out.println(" " + lEmailRecords[i].getEmailAddress());
                  
            
            }
            
            
      }
      
      /**
       * @param args
       */
      public static void main(String[] args) {
            
            // TODO Auto-generated method stub
            EmailMain lEmainMain = new EmailMain();
            lEmainMain.read();
            lEmainMain.print();

      }

}

And I got the output as I wanted:

The values in Email object are: a@a.com08007b@b.com*

 The values in Email object are: a@a.com08007b@b.com*

 The values in Email object are: a@a.com08007b@b.com*

 The values in Email object are: a@a.com08007b@b.com*

 The values in Email object are: a@a.com08007b@b.com*

 a@a.com
 a@a.com
 a@a.com
 a@a.com
 a@a.com

Let me know what you think..

Thanks.
_Esam
0
 
CEHJCommented:
>>I guess CEHJ is gone for the day :)

Sorry - i should have warned you ;-)
0
 
_EsamAuthor Commented:
Yes mayankeagle,

You did mention that... but I guess I overlooked..:)

Thanks.. both...

_Esam...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.