What you want is the Arrays.toString(int[]) method:

import java.util.Arrays;

int[] array = new int[lnr.getLineNumber() + 1];
int i = 0;

..      

System.out.println(Arrays.toString(array));

There is a static Arrays.toString helper method for every different primitive java type; the one for int[] says this:

public static String toString(int[] a)

Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(int). Returns "null" if a is null.

Answer from Sbodd on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java string › array to string conversions
Array to String Conversions | Baeldung
September 16, 2026 - Additionally, to convert an array of integers, we can use the same approach but instead call Integer.valueOf(intArray[i]) when appending to our StringBuilder. Java 8 and above offers the String.join() method that produces a new string by joining elements and separating them with the specified delimiter, in our case just empty string:
🌐
W3docs
w3docs.com › home › code snippets › java › how to convert an int array to string with tostring method in java
How to convert an int array to String with toString method in Java | W3docs
For multi-dimensional arrays, use Arrays.deepToString() instead. You can also use the String.join() method to convert an int array to a string, separating the elements with a delimiter.
Top answer
1 of 9
93

Here's what I came up with. There are several ways to do this and they depend on the tools you are using.


Using StringUtils and ArrayUtils from Commons Lang:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");

You can't just use StringUtils.join(arr, " - "); because StringUtils doesn't have that overloaded version of method. Though, it has method StringUtils.join(int[], char).

Works at any Java version, from 1.2.


Using Java 8 streams:

Something like this:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = Arrays.stream(arr)
        .mapToObj(String::valueOf)
        .collect(Collectors.joining(" - "));

In fact, there are lot of variations to achieve it using streams.

Java 8's method String.join() works only with strings, so to use it you still have to convert int[] to String[].

Use with AIString[] sarr = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);
String result = String.join(" - ", sarr);

If you're stuck using Java 7 or earlier with no libraries, you could write your own utility method:

Use with AIpublic static String myJoin(int[] arr, String separator) {
    if (null == arr || 0 == arr.length) return "";

    StringBuilder sb = new StringBuilder(256);
    sb.append(arr[0]);

    //if (arr.length == 1) return sb.toString();

    for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);

    return sb.toString();
}

Then you can do:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = myJoin(arr, " - ");
2 of 9
16

Java 8 Solution would be like this:

Use with AIStream.of(1,2,3,4).map(String::valueOf).collect(Collectors.joining("-"))
🌐
Scaler
scaler.com › home › topics › array to string in java
Array to String In Java | Scaler Topics
June 2, 2024 - We can use java stream API to convert an array to a string. In java 8 and above versions of java we have String.join() method which creates a new string by joining elements and separating them with a specified delimiter.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 03 › how-to-convert-array-to-comma-separated-string-in-java.html
How to Convert an Array to Comma Separated String in Java - Example Tutorial
June 22, 2025 - The simplest way to convert an ... is to create a StringBuilder, iterate through the array, and add each element of the array into StringBuilder after appending the comma. You just need Java 1.5 for that, even if you are not running on Java 5, you can use StringBuffer in place of StringBuilder. The joining of String has got even easier in JDK 8, where you have got the join() method right in the ...
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › strings in java › array to string in java
Array to String in Java
November 27, 2023 - So, the + is your go-to for linking strings together. To make a whole array into a single string, you just loop through the array, using + to add each element to your string, and voilà – you get a big, nice string! Converting an array to string in Java doesn't have to be a head-scratcher.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › convert-array-of-integers-to-comma-separated-string
Convert Array of Integers to Comma-Separated String - GeeksforGeeks
December 13, 2023 - If the current integer is the second last element of array, then append " and " to the answer. Else, append a comma and a space to the answer. After all the iterations return the answer string.
🌐
Medium
medium.com › javarevisited › nuanced-string-joining-in-java-7db547c331d0
Nuanced String Joining in Java | Javarevisited
April 23, 2024 - The method does not require the elements in the array to be some type of String or CharSequence already. The prefix comes first after the array parameter, followed by delimiter and then by suffix. Instead of an array of Integer, let’s switch to an array of int, and see what options we have to convert to a delimited String.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 416765 › conversion-of-int-array-to-string
java - conversion of int array to string [SOLVED] | DaniWeb
March 10, 2012 - int[] a = {1, 2, 3}; StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { if (i > 0) sb.append(','); sb.append(a[i]); } String s = sb.toString(); // "1,2,3" Pre-sizing the StringBuilder when the array is large (estimate ...
🌐
Baeldung
baeldung.com › home › java › java array › join array of primitives with separator in java
Join Array of Primitives with Separator in Java | Baeldung
January 8, 2024 - When joining our char array with this method, we must first wrap the char array into CharBuffer and then project it to char again. This is because the chars() method returns a Stream of int values. Unfortunately, the Java Stream API does not provide a native method for wrapping a Stream of char. ... String joined = CharBuffer.wrap(charArray).chars() .mapToObj(intValue -> String.valueOf((char) intValue)) .collect(Collectors.joining(separator));
🌐
Java Guides
javaguides.net › 2018 › 12 › java-convert-array-to-string-snippet.html
Java Convert Array to String [Snippet]
December 12, 2018 - Java Programs Kotlin Programs C Programs R Programs Python Programs C++ Programs Go Programs TypeScript Programs JavaScript Programs ... In this quick article, we’re going to look at different ways to convert an array of strings or integers to a string. Arrays.toString() returns a string with the content of the input array. The new string created is a comma-delimited list of the array’s elements, surrounded with square brackets (“[]”):
🌐
CodingTechRoom
codingtechroom.com › question › convert-array-of-integers-to-string-java
How to Convert an Array of Integers to a String in Java? - CodingTechRoom
Solution: Check if the array is empty and handle accordingly before conversion. Mistake: Forgetting to remove the last comma when constructing the string manually. Solution: Ensure to append a separator (like a comma) only between elements, not after the last element. ... A broad desk reference for the Java language and standard library.
🌐
Codemia
codemia.io › home › knowledge hub › how to convert an int array to string with tostring method in java
How to convert an int array to String with toString method in Java | Codemia
July 31, 2025 - Converting an integer array to a string representation is a common requirement when dealing with data display and logging. In Java, this can be accomplished using the toString method from the Java API. Although the toString method is not directly applicable to arrays, the Arrays utility class ...
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_tostring_int.htm
Java.util.Arrays.toString(int[ ]) Method
Find the perfect tutorial for your learning journey · The java.util.Arrays.toString(int[]) method returns a string representation of the contents of the specified int array. The string representation consists of a list of the array's elements, ...
🌐
Hostman
hostman.com › tutorials › converting-arrays-and-strings-in-java
Java Array to String and String to Array Conversion Guide
It covers various methods, including built-in Java functions and manual conversion techniques using loops and other string manipulation tools. The Arrays class in Java provides a convenient method for converting arrays to strings.