input characters from a string into an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harry

    input characters from a string into an array

    Folks
    I am trying to input the characters of a string into an array. The array
    needs to hold strings, so I need to convert the characters into strings and
    then input them into the array. I run into a dereferencing error

    StringBuffer buffy = new StringBuffer(st rEntered);
    for( i = 0; i < buffy.length(); i++ )
    strTemp = (buffy.charAt( i ).toString); //throw dereferencing error in
    compile
    arrayEntered[ i ] = strTemp

    Tried this a number of ways '(buffy.charAt( i ).toString); ' but either
    throws exception or compiler cannot understand

    Any suggestions would be appreciated.

    Thanks
    Harry



  • cornflux

    #2
    Re: input characters from a string into an array

    What type/object does charAt() return?


    Harry wrote:[color=blue]
    > Folks
    > I am trying to input the characters of a string into an array. The array
    > needs to hold strings, so I need to convert the characters into strings and
    > then input them into the array. I run into a dereferencing error
    >
    > StringBuffer buffy = new StringBuffer(st rEntered);
    > for( i = 0; i < buffy.length(); i++ )
    > strTemp = (buffy.charAt( i ).toString); //throw dereferencing error in
    > compile
    > arrayEntered[ i ] = strTemp
    >
    > Tried this a number of ways '(buffy.charAt( i ).toString); ' but either
    > throws exception or compiler cannot understand
    >
    > Any suggestions would be appreciated.
    >
    > Thanks
    > Harry
    >
    >
    >[/color]

    Comment

    • Josef K

      #3
      Re: input characters from a string into an array

      "Harry" <mackeyha2@hotm ail.com> skrev i meddelandet
      news:0OacneiY3Y Zjx-qiU-KYuQ@comcast.co m...[color=blue]
      > Folks
      > I am trying to input the characters of a string into an array. The array
      > needs to hold strings, so I need to convert the characters into strings[/color]
      and[color=blue]
      > then input them into the array. I run into a dereferencing error
      >
      > StringBuffer buffy = new StringBuffer(st rEntered);
      > for( i = 0; i < buffy.length(); i++ )
      > strTemp = (buffy.charAt( i ).toString); //throw dereferencing error in
      > compile
      > arrayEntered[ i ] = strTemp
      >
      > Tried this a number of ways '(buffy.charAt( i ).toString); ' but either
      > throws exception or compiler cannot understand
      >
      > Any suggestions would be appreciated.
      >
      > Thanks
      > Harry
      >[/color]

      charAt returns a primitive char. There is no such thing as a toString for
      primitives. Try this:
      strTemp = (new Character(buffy .charAt( i )).toString);
      Finally an advice. If you use a JavaIDE you will have access to "code
      insight", which will tell you which methods can be used at a certain point
      in your code.
      /Josef


      Comment

      • @  ya h o o.c o m

        #4
        Re: input characters from a string into an array

        Try
        StringBuffer buffy = new StringBuffer(st rEntered);
        for( i = 0; i < buffy.length(); i++ )
        strTemp = (buffy.charAt( i ) + "");
        arrayEntered[ i ] = strTemp;


        [color=blue]
        > "Harry" <mackeyha2@hotm ail.com> skrev i meddelandet
        > news:0OacneiY3Y Zjx-qiU-KYuQ@comcast.co m...
        >[color=green]
        >>Folks
        >>I am trying to input the characters of a string into an array. The array
        >>needs to hold strings, so I need to convert the characters into strings[/color]
        >
        > and
        >[color=green]
        >>then input them into the array. I run into a dereferencing error
        >>
        >>StringBuffe r buffy = new StringBuffer(st rEntered);
        >> for( i = 0; i < buffy.length(); i++ )
        >> strTemp = (buffy.charAt( i ).toString); //throw dereferencing error in
        >>compile
        >> arrayEntered[ i ] = strTemp
        >>
        >>Tried this a number of ways '(buffy.charAt( i ).toString); ' but either
        >>throws exception or compiler cannot understand
        >>
        >>Any suggestions would be appreciated.
        >>
        >>Thanks
        >>Harry
        >>[/color]
        >
        >
        > charAt returns a primitive char. There is no such thing as a toString for
        > primitives. Try this:
        > strTemp = (new Character(buffy .charAt( i )).toString);
        > Finally an advice. If you use a JavaIDE you will have access to "code
        > insight", which will tell you which methods can be used at a certain point
        > in your code.
        > /Josef
        >
        >[/color]

        Comment

        Working...