?
Solved

Output values from Arrays of Objects

Posted on 2006-05-25
16
Medium Priority
?
236 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:_Esam
  • 10
  • 5
16 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 16764656
>>
 lEmailRecords = new Email[lDataArrayList.size()];
                 lEmailRecords = ( Email[] ) lDataArrayList.toArray(lEmailRecords);
>>

should be *outside* the loop
0
 

Author Comment

by:_Esam
ID: 16764742
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
 

Author Comment

by:_Esam
ID: 16764825
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 16764835
When and where are you calling print?
0
 

Author Comment

by:_Esam
ID: 16764847
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16764902
>>lem = new Email(lDataLine);

may not be initializing the members correctly
0
 

Author Comment

by:_Esam
ID: 16764929
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16765048
How is your getEmailAddress() method defined?
0
 

Author Comment

by:_Esam
ID: 16765069
Like so:

public String getEmailAddress()
{
    return mEmailAddress;
}

In the Email class.

_Esam
0
 

Author Comment

by:_Esam
ID: 16765087
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
 

Author Comment

by:_Esam
ID: 16765156
Well, I will check the email in Half an hour again..
Please do drop a line...

Thanks..
_Esam
0
 

Author Comment

by:_Esam
ID: 16765548
I guess CEHJ is gone for the day :)
I will check tomorrow then...

_Esam
0
 

Author Comment

by:_Esam
ID: 16765912
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 600 total points
ID: 16767506
I'd told you in your previous question to keep it outside the loop :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16767516
>>I guess CEHJ is gone for the day :)

Sorry - i should have warned you ;-)
0
 

Author Comment

by:_Esam
ID: 16769044
Yes mayankeagle,

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

Thanks.. both...

_Esam...
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question