input string characters into an array

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

    input string characters 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


  • Badhri

    #2
    Re: input string characters into an array

    "Harry" <mackeyha2@hotm ail.com> wrote in message news:<Zl6dncZn4 KuRz-qiU-KYvg@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 and
    > then input them into the array. I run into a dereferencing error
    >[/color]
    You need not convert the string to stringBuffer as String has charAt.[color=blue]
    > StringBuffer buffy = new StringBuffer(st rEntered);
    > for( i = 0; i < buffy.length(); i++ )
    > strTemp = (buffy.charAt( i ).toString); //throw dereferencing error in
    > compile[/color]
    buffy.charAt( i ) returns a character[color=blue]
    > arrayEntered[ i ] = strTemp[/color]

    Try this

    char[] ch = new char[1];
    for( i = 0; i <strEntered.len gth(); i++ ){
    ch[0] = strEntered.char At(i);
    arrayEntered[i] = new String(ch);
    }
    [color=blue]
    >
    > 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

    • Amey Samant

      #3
      Re: input string characters into an array

      "Harry" <mackeyha2@hotm ail.com> wrote in message news:<Zl6dncZn4 KuRz-qiU-KYvg@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 and
      > then input them into the array. I run into a dereferencing error[/color]

      hi
      check this as well
      use wrapper classes

      for(int i
      Character

      Comment

      • Amey Samant

        #4
        Re: input string characters into an array

        hi Harry
        sorry my previous post was incomplete ....
        use wrapper classes

        Character ch;
        for(i=0;i<strEn trd.length();i+ +)
        {
        ch=new Character(strEn trd.charAt(i));
        arrEntrd[i]=ch.toString();
        }

        or on-the-fly like

        for(i=0;i<strEn trd.length();i+ +)
        {
        arrEntrd[i]=(new Character(strEn trd.charAt(i))) .toString();
        }

        this should do the job

        Comment

        Working...