Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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}");
    }
}
share|improve this question
2  
If you need only sorting, you can write your own Comparator for StringBuffer. – yatul May 11 '12 at 14:00
3  
What have you tried? – Matt Ball May 11 '12 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. – mwcz May 11 '12 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"}? – Marko Topolnik May 11 '12 at 14:23
No. Only {One, Three} and ignore other values.. – Sankar V May 11 '12 at 14:28
show 1 more comment

4 Answers

up vote 6 down vote accepted

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.

share|improve this answer
Thank you so much. but my stringbuffer array contains the null values at the end and it shows NullPointerException. – Sankar V May 11 '12 at 14:05
4  
@SankarVaiyapuri - well test for null and deal with them. You know how to write Java ... don't you? – Stephen C May 11 '12 at 14:19
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));
share|improve this answer

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.

share|improve this answer

How about:

String[] str = new String[stringBufferArray.length];
for (int i = 0; i < stringBufferArray.length; i++)
  str[i] = stringBufferArray[i].toString()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.