0

I have to make a getArrayString(int[] array, char separator) method which return a string where each array entry (except the last one) is followed by the specified separator.

I know the structor of the arrays methods but in this one first I have to fined a formula for returning array except the last one and second convert my int[] array to String and return it with separator.

public static getArrayString(int[] array, char separator) {
    if(array == null ||array.length == 0) {
        return null;
    }
    int size =0;
    for(int i =0; i < array.length; i++) {

        size++;
    }
    int[] newArray2 = new int[size];
    for(int i= 0, position = 0; i < array.length; i++) {
        newArray2[position]=i;
        position++;
        System.out.print(i+',');
    }
    //  String StArray = Arrays.toString(newArray2);
    //  return StArray + separator;
    //  newArray2 = convertStringArrayToString(StArray,separator);
    //  return StArray;
    //  
    //return Arrays.toString(newArray2)+ separator;
}
// private static String convertStringArrayToString(String[] newArray,char separator) {
//   StringBuilder sb = new StringBuilder();
//   for(String StArray : newArray);
//   return sb.substring(0,sb.length()-1);
// }

The comments are ideas of converting to String and returning it!

2 Answers 2

2

Joining int[] to a String

Edit: I just spotted you have an int[] not a String[]. I would probably use the Streams approach:

String result = Arrays.stream(array)
    .mapToObj(String::valueOf)
    .collect(Collectors.joining(separator));

If you really want to use a for-loop, you should use a StringJoiner:

public static getArrayString(int[] array, char separator) {
    if(array == null || array.length == 0) return "";

    String separatorString = String.valueOf(separator);
    StringJoiner sj = new StringJoiner(separatorString);

    for(int element : array) {
        sj.add(String.valueOf(element));
    }

    return sj.toString();
}

Joining String[] to a String

The phrasing of your question makes me think you're studying this for a class, but if you're not, just use the in-built join function:

// join takes Iterable<? extends CharSequence> or vararg
String.join(separator, myArray)
String.join(separator, myList)
String.join(separator, charSeq1, charSeq2, ..., charSeqN)

If you need prefix/suffix, you can use a StringJoiner:

StringJoiner sj = new StringJoiner(separator, prefix, suffix);
String result = sj.add(s1).add(s2).add(s3).toString();

...which is also the basis of the Stream Collectors.joining:

String result = Arrays.stream(array)
    .collect(Collectors.joining(separator));

String result = Arrays.stream(array)
    .collect(Collectors.joining(separator, prefix, suffix));
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your help. Yes I'm studying. And unfortunately, I think the join function is not required to use!
Maybe this will help you in a future project :)
1
public static String getArrayString(int[] array, char separator) {
    if(array == null || array.length == 0) {
        return null;
    }

    String str = "";
    for(int i = 0; i < array.length-1; i++) {
        str += "" + array[i]+ separator;
    }

    // add the last one
    str += array[array.length - 1];
    return str;
}

4 Comments

String concatenation in a loop is not a good idea. Use StringBuilder instead.
thanks a lot for the help, but I get error on the line 8 starting with str +=....from eclipse that says "cannot invoke toString() on the primitive type int." and line 12(starting with return...) error says "cannot convert from string to int[]"
You can use String.valueOf(myInt)
@AbdessamadAzzouzi - Please update your original answer :)

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.