Parse Array of Objects - string delimiter java

Discussion in 'Java' started by Pedro Rocha, Dec 5, 2004.

  1. Pedro Rocha

    Pedro Rocha Guest

    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
     
    Pedro Rocha, Dec 5, 2004
    #1
    1. Advertising

  2. Pedro Rocha

    Ann Guest

    "Pedro Rocha" <> wrote in message
    news:...
    > 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;
    }
     
    Ann, Dec 5, 2004
    #2
    1. Advertising

  3. Pedro Rocha

    Ann Guest

    "Ann" <> wrote in message
    news:7Gwsd.512364$D%.19032@attbi_s51...
    >
    > "Pedro Rocha" <> wrote in message
    > news:...
    > > 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;
    }
     
    Ann, Dec 5, 2004
    #3
  4. Pedro Rocha

    Jim Cheng Guest

    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:...
    >> > 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;
    > }
    >
    >
     
    Jim Cheng, Dec 5, 2004
    #4
  5. 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)
    sb.append(breed)
    sb.append(ages)

    return sb.toString()

    --
    Starshine Moonbeam
    mhm31x9 Smeeter#29 WSD#30
    sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM
     
    Starshine Moonbeam, Dec 5, 2004
    #5
  6. "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();
     
    Boudewijn Dijkstra, Dec 5, 2004
    #6
  7. Pedro Rocha

    marvado Guest

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

    "Boudewijn Dijkstra" <> wrote in message
    news:41b2e4e1$0$44093$...
    > "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();
    >
    >
     
    marvado, Dec 5, 2004
    #7
  8. marvado a écrit :
    > seems to work,
    > all suggestions are valid for me
    > thank you !
    >
    > "Boudewijn Dijkstra" <> wrote in message
    > news:41b2e4e1$0$44093$...
    >
    >>"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).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);
     
    =?ISO-8859-1?Q?S=E9bastien_Auvray?=, Dec 7, 2004
    #8
  9. "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$...
    >>
    >>>"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).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;
     
    Boudewijn Dijkstra, Dec 8, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    19
    Views:
    1,191
    Daniel Vallstrom
    Mar 15, 2005
  2. Replies:
    18
    Views:
    5,511
    Michael Jung
    Aug 11, 2013
  3. padmagvs
    Replies:
    1
    Views:
    452
    cuffJ
    Aug 16, 2010
  4. Valentino Lun

    convert string to array with delimiter

    Valentino Lun, Nov 5, 2008, in forum: Ruby
    Replies:
    3
    Views:
    139
    Heesob Park
    Nov 5, 2008
  5. Replies:
    3
    Views:
    106
    Brian McCauley
    Apr 26, 2007
Loading...

Share This Page