Parse Array of Objects - string delimiter java

P

Pedro Rocha

How can I parse an object from an array of objects
into a comma delimitted String.

ex:
definition
Cat (String cName, StringcDesc , String cAge)

Cat [] arrayOfCatObjects;

Say I want all the Cat objects into a String (comma delimitted)

something like:
"cat1,stray,8,cat2,bluecat,3"

Any ideas?

thanks
 
A

Ann

Pedro Rocha said:
How can I parse an object from an array of objects
into a comma delimitted String.

ex:
definition
Cat (String cName, StringcDesc , String cAge)

Cat [] arrayOfCatObjects;

Say I want all the Cat objects into a String (comma delimitted)

something like:
"cat1,stray,8,cat2,bluecat,3"

Any ideas?

thanks
---------------not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
catlist += Cat[j].cName;
}
 
A

Ann

Ann said:
Pedro Rocha said:
How can I parse an object from an array of objects
into a comma delimitted String.

ex:
definition
Cat (String cName, StringcDesc , String cAge)

Cat [] arrayOfCatObjects;

Say I want all the Cat objects into a String (comma delimitted)

something like:
"cat1,stray,8,cat2,bluecat,3"

Any ideas?

thanks

oops forgot the comma
---------------still not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName;
}
 
J

Jim Cheng

Pedro is asking fir a String (comma delimitted) including the name, desc,
and age.
So the following does not work.
---------------still not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName;
}

try:

String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName +","+ Cat[j].cDesc +","+ cat[j].cAge;
}


Jim


Ann said:
Ann said:
Pedro Rocha said:
How can I parse an object from an array of objects
into a comma delimitted String.

ex:
definition
Cat (String cName, StringcDesc , String cAge)

Cat [] arrayOfCatObjects;

Say I want all the Cat objects into a String (comma delimitted)

something like:
"cat1,stray,8,cat2,bluecat,3"

Any ideas?

thanks

oops forgot the comma
---------------still not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName;
}
 
S

Starshine Moonbeam

Pedro said:
How can I parse an object from an array of objects
into a comma delimitted String.

ex:
definition
Cat (String cName, StringcDesc , String cAge)

Cat [] arrayOfCatObjects;

Say I want all the Cat objects into a String (comma delimitted)

something like:
"cat1,stray,8,cat2,bluecat,3"

Any ideas?

thanks

String[] knownNames {"fluffy","unknown","scratch"}
String[] breed {"tabby","siamese","calico"}
String[] ages {"8", "10","12"}


public String toString

for ()

StringBuffer sb = new StringBuffer();

sb.append(knownNames)
sb.append(breed)
sb.append(ages)

return sb.toString()
 
B

Boudewijn Dijkstra

Jim Cheng said:
Pedro is asking fir a String (comma delimitted) including the name, desc,
and age.
So the following does not work.
---------------still not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName;
}

try:

String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName +","+ Cat[j].cDesc +","+ cat[j].cAge;
}

StringBuffer buf = new StringBuffer(cat.length * 30);

buf.append(cat[0].cName).append(',').append(cat[0].cDesc)
.append(',').append(cat[0].cAge);
for (int j = 1; j < cat.length; j++)
{
buf.append(',').append(cat[j].cName).append(',')
.append(cat[j].cDesc).append(',').append(cat[j].cAge);
}

String catlist = buf.toString();
 
M

marvado

seems to work,
all suggestions are valid for me
thank you !

Boudewijn Dijkstra said:
Jim Cheng said:
Pedro is asking fir a String (comma delimitted) including the name, desc,
and age.
So the following does not work.
---------------still not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName;
}

try:

String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName +","+ Cat[j].cDesc +","+ cat[j].cAge;
}

StringBuffer buf = new StringBuffer(cat.length * 30);

buf.append(cat[0].cName).append(',').append(cat[0].cDesc)
.append(',').append(cat[0].cAge);
for (int j = 1; j < cat.length; j++)
{
buf.append(',').append(cat[j].cName).append(',')
.append(cat[j].cDesc).append(',').append(cat[j].cAge);
}

String catlist = buf.toString();
 
?

=?ISO-8859-1?Q?S=E9bastien_Auvray?=

marvado a écrit :
seems to work,
all suggestions are valid for me
thank you !

Pedro is asking fir a String (comma delimitted) including the name, desc,
and age.
So the following does not work.

---------------still not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName;
}



try:

String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName +","+ Cat[j].cDesc +","+ cat[j].cAge;
}

StringBuffer buf = new StringBuffer(cat.length * 30);

buf.append(cat[0].cName).append(',').append(cat[0].cDesc)
.append(',').append(cat[0].cAge);
for (int j = 1; j < cat.length; j++)
{
buf.append(',').append(cat[j].cName).append(',')
.append(cat[j].cDesc).append(',').append(cat[j].cAge);
}

String catlist = buf.toString();
Hi !
It could also be beautiful to override Cat.toString, and use
Collection.toString()...

i.e.
for Cat
String toString()
{
return new
StringBuffer(cName).append(',').append(cDesc).append(',').append(cAge)
}

then
// Produces [ cat1, desc1, age1, cat2, desc2, age2 ]
String result_l = Arrays.asList(arrayOfCatObjects).toString();
// Remove 1st and last character [ & ]
result_l = result_l.substring(1, result_l.length-1);
 
B

Boudewijn Dijkstra

Sébastien Auvray said:
marvado a écrit :
seems to work,
all suggestions are valid for me
thank you !

"Jim Cheng" <[email protected]> schreef in bericht

Pedro is asking fir a String (comma delimitted) including the name, desc,
and age.
So the following does not work.

---------------still not tested ---------------
String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName;
}



try:

String catlist;

for (int j = 0; j < Cat.length; j++)
{
if(j!=0) catlist += ",";
catlist += Cat[j].cName +","+ Cat[j].cDesc +","+ cat[j].cAge;
}

StringBuffer buf = new StringBuffer(cat.length * 30);

buf.append(cat[0].cName).append(',').append(cat[0].cDesc)
.append(',').append(cat[0].cAge);
for (int j = 1; j < cat.length; j++)
{
buf.append(',').append(cat[j].cName).append(',')
.append(cat[j].cDesc).append(',').append(cat[j].cAge);
}

String catlist = buf.toString();
Hi !
It could also be beautiful to override Cat.toString, and use
Collection.toString()...

i.e.
for Cat
String toString()
{
return new
StringBuffer(cName).append(',').append(cDesc).append(',').append(cAge)
}

In this case it would be unnesscary to explicitly use StringBuffer, the
following statement is just as effecient and a lot more readable:
return cName + ',' + cDesc + ',' + cAge;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top