casting int as String

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B J H

    casting int as String

    I want to do the following

    int [] list;
    String list;
    i int;
    .....
    for (i=0; i<int.length, i++_
    {
    list.concat(lis t[i]);
    list.concat(" ");
    }

    But list.concat expects a string, and I'm putting a int into it - how can I
    make this work? How can I put an int into string.concat?

    Thanks



  • kevinc

    #2
    Re: casting int as String

    Substitute

    String list

    with

    StringBuffer list = new StringBuffer();

    list.append(lis t[i]);

    When you want the resultant string

    do list.toString() ;




    B J H wrote:[color=blue]
    > I want to do the following
    >
    > int [] list;
    > String list;
    > i int;
    > ....
    > for (i=0; i<int.length, i++_
    > {
    > list.concat(lis t[i]);
    > list.concat(" ");
    > }
    >
    > But list.concat expects a string, and I'm putting a int into it - how can I
    > make this work? How can I put an int into string.concat?
    >
    > Thanks
    >
    >
    >[/color]

    Comment

    • Brad BARCLAY

      #3
      Re: casting int as String

      B J H wrote:
      [color=blue]
      > list.concat(lis t[i]);[/color]

      What are you trying to concatenate here? A string representing the
      numerical digits in the int (ie, if list[i]=12345, you want to append
      "12345" to your String), _or_ do you want to append the character with
      the matching Unicode (or current codepage) value to your String (ie: if
      list[i]=65, you want to append "A" to your String)?

      If you can explain what it is you're trying to achieve, someone should
      be able to help you better.

      Brad BARCLAY

      --
      =-=-=-=-=-=-=-=-=
      From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
      The jSyncManager Project: http://www.jsyncmanager.org

      Comment

      • B J H

        #4
        Re: casting int as String

        Sorry,

        I have an array of integers. I want to create a string of these integers
        with a space in between each one.

        E.g.

        int [] intarray;
        String integerlist;

        If the array contains say
        10
        12
        15
        45
        51

        then i want the string to look like this
        "10 12 15 45 51"

        Any suggestions ?

        "Brad BARCLAY" <bbarclay@jsync manager.org> wrote in message
        news:KiOwb.4376 $zJq.3743@news0 1.bloor.is.net. cable.rogers.co m...[color=blue]
        > B J H wrote:
        >[color=green]
        > > list.concat(lis t[i]);[/color]
        >
        > What are you trying to concatenate here? A string representing the
        > numerical digits in the int (ie, if list[i]=12345, you want to append
        > "12345" to your String), _or_ do you want to append the character with
        > the matching Unicode (or current codepage) value to your String (ie: if
        > list[i]=65, you want to append "A" to your String)?
        >
        > If you can explain what it is you're trying to achieve, someone should
        > be able to help you better.
        >
        > Brad BARCLAY
        >
        > --
        > =-=-=-=-=-=-=-=-=
        > From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
        > The jSyncManager Project: http://www.jsyncmanager.org[/color]


        Comment

        • Anthony Borla

          #5
          Re: casting int as String


          "B J H" <no@one.com> wrote in message
          news:QQQwb.2391 09$pf5.1692449@ news.easynews.c om...[color=blue]
          > Sorry,
          >
          > I have an array of integers. I want to create a string of
          > these integers with a space in between each one.
          >
          > E.g.
          >
          > int [] intarray;
          > String integerlist;
          >
          > If the array contains say
          > 10
          > 12
          > 15
          > 45
          > 51
          >
          > then i want the string to look like this
          > "10 12 15 45 51"
          >
          > Any suggestions ?
          >[/color]

          int[] intarray = {10, 12, 15, 45, 51 };

          StringBuffer spaceSepInteger s =
          new StringBuffer(in tarray.length * 2);

          for (int i = 0; i < intarray.length ; ++i)
          spaceSepInteger s.append(Intege r.toString(inta rray[i])).append(" ");

          String integerlist = spaceSepInteger s.toString().tr im();

          Crude but simple !

          I hope this helps.

          Anthony Borla


          Comment

          • B J H

            #6
            Re: casting int as String

            Thank you. I finally implemented it like this...

            public String displayMarks()
            {
            StringBuffer list = new StringBuffer();
            for (i=0; i < nummark; i++)
            {
            list.append(mar ks[i]);
            list.append(" ");
            }
            return list.toString() ;
            }

            This seems to work fine me at the moment. The length of the array is
            unknown, I hope that I don't run into problems if the array turns out to be
            hundreds of integers long! :)

            Thanks.



            "Anthony Borla" <ajborla@bigpon d.com> wrote in message
            news:1yRwb.2717 0$aT.6456@news-server.bigpond. net.au...[color=blue]
            >
            > "B J H" <no@one.com> wrote in message
            > news:QQQwb.2391 09$pf5.1692449@ news.easynews.c om...[color=green]
            > > Sorry,
            > >
            > > I have an array of integers. I want to create a string of
            > > these integers with a space in between each one.
            > >
            > > E.g.
            > >
            > > int [] intarray;
            > > String integerlist;
            > >
            > > If the array contains say
            > > 10
            > > 12
            > > 15
            > > 45
            > > 51
            > >
            > > then i want the string to look like this
            > > "10 12 15 45 51"
            > >
            > > Any suggestions ?
            > >[/color]
            >
            > int[] intarray = {10, 12, 15, 45, 51 };
            >
            > StringBuffer spaceSepInteger s =
            > new StringBuffer(in tarray.length * 2);
            >
            > for (int i = 0; i < intarray.length ; ++i)
            > spaceSepInteger s.append(Intege r.toString(inta rray[i])).append(" ");
            >
            > String integerlist = spaceSepInteger s.toString().tr im();
            >
            > Crude but simple !
            >
            > I hope this helps.
            >
            > Anthony Borla
            >
            >[/color]


            Comment

            • Brad BARCLAY

              #7
              Re: casting int as String

              B J H wrote:
              [color=blue]
              > If the array contains say
              > 10
              > 12
              > 15
              > 45
              > 51
              >
              > then i want the string to look like this
              > "10 12 15 45 51"
              >
              > Any suggestions ?[/color]

              First off, you have to convert from int to String. There are a few
              ways of doing this. Assuming 'i' is an integer:

              0) Use Integer.toStrin g(i)
              1) Use (""+i), or
              2) Use something akin to the following:

              StringBuffer sb=new StringBuffer();
              sb.append(i);

              If you're going to be appending a bunch of ints together, use the last
              option (#2) above -- it's quite a bit more efficient than the other two.

              Brad BARCLAY

              --
              =-=-=-=-=-=-=-=-=
              From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
              The jSyncManager Project: http://www.jsyncmanager.org
              

              Comment

              Working...