Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Parse Array of Objects - string delimiter java

Reply
Thread Tools

Parse Array of Objects - string delimiter java

 
 
Pedro Rocha
Guest
Posts: n/a
 
      12-05-2004
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
 
Reply With Quote
 
 
 
 
Ann
Guest
Posts: n/a
 
      12-05-2004

"Pedro Rocha" <> wrote in message
news: om...
> 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;
}


 
Reply With Quote
 
 
 
 
Ann
Guest
Posts: n/a
 
      12-05-2004

"Ann" <> wrote in message
news:7Gwsd.512364$D%.19032@attbi_s51...
>
> "Pedro Rocha" <> wrote in message
> news: om...
> > 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;
}


 
Reply With Quote
 
Jim Cheng
Guest
Posts: n/a
 
      12-05-2004

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" <> wrote in message
news:eMwsd.442637$wV.427057@attbi_s54...
>
> "Ann" <> wrote in message
> news:7Gwsd.512364$D%.19032@attbi_s51...
>>
>> "Pedro Rocha" <> wrote in message
>> news: om...
>> > 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;
> }
>
>



 
Reply With Quote
 
Starshine Moonbeam
Guest
Posts: n/a
 
      12-05-2004
In article < >, Pedro
Rocha () dropped a +5 bundle of words...

> 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[i])
sb.append(breed[i])
sb.append(ages[i])

return sb.toString()

--
Starshine Moonbeam
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM









 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      12-05-2004
"Jim Cheng" <> schreef in bericht
news:coucu9$rko$...
>
> 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();


 
Reply With Quote
 
marvado
Guest
Posts: n/a
 
      12-05-2004
seems to work,
all suggestions are valid for me
thank you !

"Boudewijn Dijkstra" <> wrote in message
news:41b2e4e1$0$44093$ i.nl...
> "Jim Cheng" <> schreef in bericht
> news:coucu9$rko$...
>>
>> 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();
>
>



 
Reply With Quote
 
=?ISO-8859-1?Q?S=E9bastien_Auvray?=
Guest
Posts: n/a
 
      12-07-2004
marvado a écrit :
> seems to work,
> all suggestions are valid for me
> thank you !
>
> "Boudewijn Dijkstra" <> wrote in message
> news:41b2e4e1$0$44093$ i.nl...
>
>>"Jim Cheng" <> schreef in bericht
>>news:coucu9$rko$...
>>
>>>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).appe nd(',').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);
 
Reply With Quote
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      12-08-2004
"Sébastien Auvray" <> schreef in bericht
news:41b63284$0$8030$...
> marvado a écrit :
>> seems to work,
>> all suggestions are valid for me
>> thank you !
>>
>> "Boudewijn Dijkstra" <> wrote in message
>> news:41b2e4e1$0$44093$ i.nl...
>>
>>>"Jim Cheng" <> schreef in bericht
>>>news:coucu9$rko$...
>>>
>>>>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).appe nd(',').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;



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sorting string array based on delimiter padmagvs Java 1 08-15-2010 11:21 PM
convert string to array with delimiter Valentino Lun Ruby 3 11-05-2008 09:16 AM
java String split() does not work for delimiter "|" ? chunji08@gmail.com Java 5 10-13-2007 06:31 AM
parse with multiple delimiter anitawa@gmail.com Perl Misc 3 04-26-2007 08:12 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57