0

I keep getting this error while trying to convert to CharSequence[].

The method toArray(CharSequence[]) is undefined for the type String

This is my code

              CharSequence[] cs = abbrev.toArray(new CharSequence[abbrev.length()]);

The abbrev is just splitting sentences into its first characters (Hello World --> HW)

       String[] result = matches.toString().split("\\s+");
        // The string we'll create

        String abbrev = "";

           // Loop over the results from the string splitting
           for (int i = 0; i < result.length; i++){

               // Grab the first character of this entry
               char c = result[i].charAt(0);

               // If its a number, add the whole number
               if (c >= '0' && c <= '9'){
                   abbrev += result[i];
               }

               // If its not a number, just append the character
               else{
                   abbrev += c;
               }
           }

Any Ideas?

Thanks

BasicCoder
  • 99
  • 1
  • 2
  • 9

2 Answers2

0

Because abbrev is a string, you can't call toArray on it. Convert it to a CharSequence first. For example,

CharSequence[] abbrevCs = abbrev; // it's converted automatically
CharSequence[] cs = abbrevCs.toArray(new CharSequence[abbrev.length()]);
0

You don't have to convert a String to a CharSequence because a String is a CharSequence. You can just assign a String object to a CharSequence object and the conversion is implicit. See here for more details: How to convert a String to CharSequence?

Community
  • 1
  • 1
vdelricco
  • 749
  • 4
  • 15