-2

Given an array of numbers (integers) how would I go about converting it to a string.

I am from JS so in we normally do :

[1 , 2 , 3 , 4 , 5].join(separator)

But Java doesn't seem to do that.

I tried :

 array.join(" ")

But that throws an error on that it cannot find symbol.


Here is the code :

class Conv {
    public static String findOut(int[] arr) {
        return arr.join(" ");
    }
}
7
  • 6
    Don't call methods that don't exist, and don't make up methods that "seem" right. Look at the API. Commented Jun 14, 2018 at 13:55
  • 3
    java.util.Arrays.toString(...) Commented Jun 14, 2018 at 13:56
  • Stream.of(arr).collect(Collectors.joining(", ")); This will add ", " as separator. Commented Jun 14, 2018 at 13:56
  • 2
    Please explain how my comment was rude. You're coding by making wild guesses, and I am suggesting that you don't do that. Please consider following the advice. Commented Jun 14, 2018 at 13:57
  • 1
    @HovercraftFullOfEels : how is this even related to what you linked ? I don't see how it explains the question ? Commented Jun 14, 2018 at 13:59

5 Answers 5

3

Arrays#toString

Java does not provide a join method for arrays. Utility methods can be found in the Arrays class. For example Arrays.toString(values) (documentation) which returns a String in the following format:

[1, 5, 15, 2]

So

String result = Arrays.toString(values);

StringJoiner

However, you explicitly want the following format:

1 5 15 2

Therefore, use the StringJoiner class (documentation):

StringJoiner sj = new StringJoiner(" ");
for (int value : values) {
    sj.add(value);
}
String result = sj.toString();

Stream API

Or use the Stream API (documentation) to achieve the same result but in a more functional-style:

String result = Arrays.stream(values)  // IntStream
    .mapToObj(Integer::toString)       // Stream<String>
    .collect(Collectors.joining(" ");  // String
Sign up to request clarification or add additional context in comments.

1 Comment

another option would be Arrays.toString(arr) .replaceAll("\\[|]", "") .replace(",", " "); or String.join(" ", IntStream.of(arr) .mapToObj(Integer::toString).collect(Collectors.toList()));. Though the best approach i believe is the joining collector you're using ;-)
2

There's no join method on arrays, instead you can use the stream API's joining collector:

Arrays.stream(arr)
      .mapToObj(Integer::toString)
      .collect(Collectors.joining(" "));

8 Comments

Thanks, So I am guessing I need to import Java.util.arrays ?
@MuhammadSalman you need two imports, import java.util.*; and import java.util.stream.*;
Thanks, I am new to Java So don't know about how this works. Thanks
Hi, When I try to use this, It gives an error : error: incompatible types: Object cannot be converted to String
@MuhammadSalman isn't arr an int[] as shown in your post?
|
1
    String out = "";
    for(int i = arr.length){
        out = out+arr[i];
    }

I think that is what you want

Comments

1

You can use Arrays.toString(int[] a) method.

int[] a = { 1, 2, 3, 4, 5 };
String b = Arrays.toString(a);
System.out.println(b);

It will print [1, 2, 3, 4, 5]

Later you can modify the string for what you want.

Don't forget to import java.util.Arrays.

You can see it at the java documentation

Comments

0

Bit late to the party here, but in case you're stuck on a pre 1.8 version of java (stream, StringJoiner etc only exist since 1.8), you can just loop through your array and append to a String. I put in a ternary operator to not add the space at the end as I assume you don't want that:

String result = "";
for (int i = 0; i < arr.length; i++) {
    result += (i == arr.length - 1) ? arr[i] : arr[i] + " ";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.