-2

I need to convert a StringBuffer Array to String Array for sorting. Is there any method in Java?

Below is my code and i am trying to sort the values(input1) in alphabetical order that contains the first letter as UpperCase and ignoring other values.

import java.util.*;
public class IPLMatch
{
    public static void main(String args[])
    {

        int input1 = 4;
        String input2[] = {"One","two","Three","foUr"}, output[];
        StringBuffer[] temp = new StringBuffer[input1];
        int j = 0, k = 0;
        for(int i = 0; i < input1; i++)
        {
            boolean isUpperCase = Character.isUpperCase(input2[i].charAt(0));
            if(isUpperCase)
            {
                temp[j++] = new StringBuffer(input2[i]);
            }
        }
                //I need convert my stringbuffer array (temp) to String array for sorting
        Arrays.sort(temp); 
        System.out.print("{");
        for(int i = 0; i < temp.length; i++)
        {
            System.out.print( temp[i]+ ",");
        }
        System.out.print("\b}");
    }
}
5
  • 2
    If you need only sorting, you can write your own Comparator for StringBuffer. Commented May 11, 2012 at 14:00
  • 2
    Please read FAQ: How To Ask before asking your next question. Putting some effort into the question will result in better answers. Commented May 11, 2012 at 14:02
  • Oh my god, what a mess of code. You don't need the StringBuffer at all. You just want to sort a string array according to the first uppercase char in those strings! Do you want your output as {"One", "Three", "foUr"}? Commented May 11, 2012 at 14:23
  • No. Only {One, Three} and ignore other values.. Commented May 11, 2012 at 14:28
  • So what you really need is just filtering and NOT sorting? Commented May 11, 2012 at 15:51

4 Answers 4

7

I don't think there's a built-in method. However, it's easy to write one:

public static String[] toStringArr(StringBuffer sb[]) {
    if (sb == null) return null;
    String str[] = new String[sb.length];
    for (int i = 0; i < sb.length; i++) {
        if (sb[i] != null) {
            str[i] = sb[i].toString();
        }
    }
    return str;
}

or, if you feel like writing a generic method:

public static<T> String[] toStringArr(T arr[]) {
    if (arr == null) return null;
    String ret[] = new String[arr.length];
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] != null) {
            ret[i] = arr[i].toString();
        }
    }
    return ret;
}

This takes any array of objects, applies toString() to every element, and returns the resulting array of strings.

2
  • Thank you so much. but my stringbuffer array contains the null values at the end and it shows NullPointerException. Commented May 11, 2012 at 14:05
  • 5
    @SankarVaiyapuri - well test for null and deal with them. You know how to write Java ... don't you? Commented May 11, 2012 at 14:19
2
final String[] temp = {"One","two","Three","foUr"};
final List<String> out = new ArrayList<String>();
for (String s : temp) if (Character.isUpperCase(s.charAt(0))) out.add(s);
final String[] outAry = out.toArray(new String[out.size()]);
Arrays.sort(outAry);
System.out.println(Arrays.toString(outAry));
0

No. There is no method available for converting an Array of one type into an Array of another type. You will need to iterate through the first Array of StringBuffers and convert each in turn into a String in a new Array of the same size.

0

How about:

String[] str = new String[stringBufferArray.length];
for (int i = 0; i < stringBufferArray.length; i++)
  str[i] = stringBufferArray[i].toString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.