Link to home
Start Free TrialLog in
Avatar of _Esam
_Esam

asked on

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.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _Esam
_Esam

ASKER

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
Avatar of _Esam

ASKER

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

When and where are you calling print?
Avatar of _Esam

ASKER

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
>>lem = new Email(lDataLine);

may not be initializing the members correctly
Avatar of _Esam

ASKER

>>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
How is your getEmailAddress() method defined?
Avatar of _Esam

ASKER

Like so:

public String getEmailAddress()
{
    return mEmailAddress;
}

In the Email class.

_Esam
Avatar of _Esam

ASKER

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
Avatar of _Esam

ASKER

Well, I will check the email in Half an hour again..
Please do drop a line...

Thanks..
_Esam
Avatar of _Esam

ASKER

I guess CEHJ is gone for the day :)
I will check tomorrow then...

_Esam
Avatar of _Esam

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>I guess CEHJ is gone for the day :)

Sorry - i should have warned you ;-)
Avatar of _Esam

ASKER

Yes mayankeagle,

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

Thanks.. both...

_Esam...