_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/Fe edback2.tx t"));
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(lEm ailRecords );
}
catch(Exception e){}
}
public void print(){
for (int i = 0; i < lEmailRecords.length; i++){
System.out.println(" " + lEmailRecords[i].getEmailA ddress()); //this did not work ???
System.out.println(" " + "In print()");
}
}
What's wrong???
Thanks
_Esam.
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/Fe
lDataArrayList = new ArrayList();
while((lDataLine = mDataFileReader.readLine()
lem = new Email(lDataLine);
System.out.println(" " + "In read()" + lem); // this worked
lDataArrayList.add(lem);
lEmailRecords = new Email[lDataArrayList.size(
lEmailRecords = ( Email[] ) lDataArrayList.toArray(lEm
}
catch(Exception e){}
}
public void print(){
for (int i = 0; i < lEmailRecords.length; i++){
System.out.println(" " + lEmailRecords[i].getEmailA
System.out.println(" " + "In print()");
}
}
What's wrong???
Thanks
_Esam.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(lEmailReco rds)
Now, on the other side,
I want to get the objects and cast it to proper type as:
messages = (Email[])pDataContent.getM essages() // 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
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(lEmailReco
Now, on the other side,
I want to get the objects and cast it to proper type as:
messages = (Email[])pDataContent.getM
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?
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
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
may not be initializing the members correctly
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
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?
ASKER
Like so:
public String getEmailAddress()
{
return mEmailAddress;
}
In the Email class.
_Esam
public String getEmailAddress()
{
return mEmailAddress;
}
In the Email class.
_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
I do get the email address with getEmailAddress if I try it in the read() method?
Doesn't work in the print method???
_Esam
ASKER
Well, I will check the email in Half an hour again..
Please do drop a line...
Thanks..
_Esam
Please do drop a line...
Thanks..
_Esam
ASKER
I guess CEHJ is gone for the day :)
I will check tomorrow then...
_Esam
I will check tomorrow then...
_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.E mail;
// com.aa.ed.cecevfeed.data.E mailBehavi or;
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/Fe edback.txt "));
while((lDataLine = mDataFileReader.readLine() ) != null){
lem = new Email(lDataLine);
lDataArrayList.add(lem);
lEmailRecords = new Email[lDataArrayList.size( )];
lEmailRecords = (Email[]) lDataArrayList.toArray(lEm ailRecords );
System.out.println(" " + lem);
};
}
catch (Exception e){
}
}
public void print(){
for (int i = 0; i < lEmailRecords.length; i++){
System.out.println(" " + lEmailRecords[i].getEmailA ddress());
}
}
/**
* @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
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.E
// com.aa.ed.cecevfeed.data.E
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/Fe
while((lDataLine = mDataFileReader.readLine()
lem = new Email(lDataLine);
lDataArrayList.add(lem);
lEmailRecords = new Email[lDataArrayList.size(
lEmailRecords = (Email[]) lDataArrayList.toArray(lEm
System.out.println(" " + lem);
};
}
catch (Exception e){
}
}
public void print(){
for (int i = 0; i < lEmailRecords.length; i++){
System.out.println(" " + lEmailRecords[i].getEmailA
}
}
/**
* @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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>I guess CEHJ is gone for the day :)
Sorry - i should have warned you ;-)
Sorry - i should have warned you ;-)
ASKER
Yes mayankeagle,
You did mention that... but I guess I overlooked..:)
Thanks.. both...
_Esam...
You did mention that... but I guess I overlooked..:)
Thanks.. both...
_Esam...
ASKER
for (int i = 0; i < lEmailRecords.length; i++){
System.out.println(" " + lEmailRecords[i].getEmailA
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