0

I'm trying to get this program's groupPairs function to take the six Strings in an initial String array [One,Two,Three,Four,Five,Six] and create a new String array of half the size (3) with the original six Strings grouped sequentially in pairs [OneTwo,ThreeFour,FiveSix], and then return that resulting new String[] to the main method.

import java.util.*;

public class Application
{
    static String[] groupPairs(String[] array)
    {
        String[] newArray = new String[(array.length)/2];
        int count=0;
        for(String string:newArray)
        {
            newArray[count]=array[count].append(array[count+1]);
            count=count+2;
        }
        return newArray;
    }
    public static void main(String args[]) //main method, don't worry about this
    {
        String[] list = new String[5];
        list[0]="One";
        list[1]="Two";
        list[2]="Three";
        list[3]="Four";
        list[4]="Five";
        list[5]="Six";
        String[] list2 = groupPairs(list);
    }
}

When trying to compile the program, I get this error:

Application.java:11: cannot find symbol
symbol  : method append(java.lang.String)
location: class java.lang.String
            newArray[count]=array[count].append(array[count+1]);
                                        ^

Any ideas on how to fix this line so that my new array will have concatenated pairs of the original String[] values would be greatly appreciated!

1 Answer 1

0

You cannot perform append operation on array. Try the following.

String[] list = new String[6];
list[0] = "One";
list[1] = "Two";
list[2] = "Three";
list[3] = "Four";
list[4] = "Five";
list[5] = "Six";
String[] list2 = new String[list.length / 2];
for (int i = 0, j = 0; i < list.length; i++, j++)
{
list2[j] = list[i] + list[++i];
}

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.