2

I have the output of my array that looks like 123611 (example numbers).
I am trying to get the output to be nicely formatted 1,2,3,6,11

I have not been around Java too much and I cannot remember how to go about doing this. Thanks!!

Here is my code:

int array3[] = { 1, 2, 3, 6, 11 };

for (int i = 0; i < array3.length; i++)
    System.out.print(array3[i]);
0

6 Answers 6

2

You can use Streams available from Java 8, Note here I'm using the integer wrapper class Integer, not the primitive type int

Integer array3[] = { 1, 2, 3, 6, 11 };

System.out.println(Stream.of(array3)
 .collect(Collectors.mapping(String::valueOf, Collectors.joining(",")))
);

As @Jelaby mentioned, it's better to use map(), which converts each Integer to String first, then join it

Integer array3[] = { 1, 2, 3, 6, 11 };

System.out.println(Stream.of(array3)
                         .map(String::valueOf)
                         .collect( Collectors.joining(",")));
Sign up to request clarification or add additional context in comments.

2 Comments

Good idea. You can get an IntStream from an int array with Arrays.stream(), and I would say that using map() or mapToObj() to transform the stream is more idiomatic than using a mapping collector. For example: Arrays.stream(array3).mapToObj(String::valueOf).collect(Collectors.joining(",")) (if using Integer instead of int you'd use map instead of mapToObj).
Oh cool, I didn't know about mapToObj method, Thank you
1

You simply need to print them as well =)

for(int i=0; i< array3.length; i++) 
    System.out.print(array3[i] + (i <array3.length-1 ? ",":"") ); 

This is will leave the trailing comma off, and print one between each entry

Comments

1

Here is an easy way to do this:

import java.util.Arrays;

public class SO {
  public static void main(String[] args){
    int[] a = {1,2,3,6,11};
    System.out.println(Arrays.toString(a));
  }
}

This outputs:

[1, 2, 3, 6, 11]

Or you can strip out the chars you don't want like this:

System.out.println(Arrays.toString(a).replaceAll("\\[|\\]|\\s", ""));

Which outputs:

1,2,3,6,11

Comments

0

You can print , after each element except last one using this loop

for (int i = 0; i < array3.length - 1; i++)
    System.out.print(array3[i] + ", ");

and later print last element of array without , after it.

System.out.println(array3[array3.length - 1]);

Or just use

String arrStr = Arrays.toString(array3);//will produce "[1, 2, 3, 6, 11]"
System.out.println(arrStr);
//if you want to remove [...] brackets you can substring them with
System.out.println(arrStr.substring(1,arrStr.length()-1));

Comments

0

I would bet this is what you're looking for:

Arrays.toString(array3);

The output is:

[1, 2, 3, 6, 11]

If you don't want the square brackets, just strip them.

Comments

0
int array3[] = { 1, 2, 3, 6, 11 };
String s ="";
for (int i = 0; i < array3.length-1; i++){
    s +=array3[i]+", ";
}
s +=array3[array3.length-1];
System.out.print(s);

is essentially the same as Array.toString() but without [], and doesn't use if statment or ternary (?:) in each iteration to check if it's the last element.In genaral, building a String first, is a better practice, because its faster and you get a string to process, System.out and its gone.

1 Comment

Use StringBuilder like stringBuilder.append(), because String is immutable every time it creates

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.