Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:59:58+00:00 2026-05-14T22:59:58+00:00

I am trying to create a method that pretty much takes anything as a

  • 0

I am trying to create a method that pretty much takes anything as a parameter, and returns a concatenated string representation of the value with some delimiter.

public static String getConcatenated(char delim, Object ...names) {
String[] stringArray = Arrays.copyOf(names, names.length, String[].class); //Exception here
return getConcatenated(delim, stringArray);
}

And the actual method

public static String getConcatenated(char delim, String ... names) {
if(names == null || names.length == 0)
    return "";

StringBuilder sb = new StringBuilder();

for(int i = 0; i < names.length; i++) {
    String n = names[i];
    if(n != null) {
        sb.append(n.trim());
        sb.append(delim);
    }
}

//Remove the last delim
return sb.substring(0, sb.length()-1).toString();
}

And I have the following JUnit test:

final String two = RedpillLinproUtils.getConcatenated(' ', "Shervin", "Asgari");
Assert.assertEquals("Result", "Shervin Asgari", two); //OK

final String three = RedpillLinproUtils.getConcatenated(';', "Shervin", "Asgari");
Assert.assertEquals("Result", "Shervin;Asgari", three); //OK

final String four = RedpillLinproUtils.getConcatenated(';', "Shervin", null, "Asgari", null);
Assert.assertEquals("Result", "Shervin;Asgari", four); //OK

final String five = RedpillLinproUtils.getConcatenated('/', 1, 2, null, 3, 4);
Assert.assertEquals("Result", "1/2/3/4", five); //FAIL

However, the test fails on the last part with the exception:

java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Arrays.copyOf(Arrays.java:2763)

Can someone spot the error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:59:58+00:00Added an answer on May 14, 2026 at 10:59 pm

    The cause

    You can’t put an Integer in a String[]. That’s why you get ArrayStoreException. Arrays.copyOf does no conversion.

    From the API:

    T[] copyOf(U[] original, int newLength, Class< ? extends T[]> newType)

    Throws: ArrayStoreException – if an element copied from original is not of a runtime type that can be stored in an array of class newType

    The ArrayStoreException API has an example:

    Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:

        Object x[] = new String[3];
        x[0] = new Integer(0);
    

    See also

    • JLS 10.10 Array Store Exception

    The fix

    That said, you actually don’t need a String... and Arrays.copyOf to a String[]. Your getConcatenated can just take Object... and it’d work just fine.

    public static String getConcatenated(char delim, Object... objs) {
        if(objs == null || objs.length == 0)
            return "";
    
        StringBuilder sb = new StringBuilder();
        for (Object o : objs) {
            if(o != null) {
                if (sb.length() > 0) sb.append(delim);
                sb.append(o.toString().trim());
            }
        }
        return sb.toString();
    }
    

    Now you can do the following:

        System.out.println(getConcatenated(';', "Shervin", null, "Asgari", null));
        // prints "Shervin;Asgari"
    
        System.out.println(getConcatenated('/', 1, 2, null, 3, 4));
        // prints "1/2/3/4"
    
        System.out.println(getConcatenated(':', "  ", null, "boo", "boo"));
        // prints "boo:boo"
    

    Note that this follows the intended specification in the original code, i.e. trim() and skips null.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an application that will let me execute a method specified
I'm trying to learn about Expression trees, and I've created a method that takes
The title pretty much says it. I have some methods that need to run
I'm trying to create a generic extension method, that works on typed data tables
I'm trying to create an Extension Method for MVC's htmlHelper. The purpose is to
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a small monitor application that displays current internet usage as percentage
Trying to create a list to return some JSON data to a view. Following
So I'm trying to create a C# WCF REST service that is called by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.