12

I have a string array which has k elements. I want to print them out using System.out.format, but the issue is that I do not know k. So essentially, I want to use something like: System.out.format("%s %s ... k times", str1, str2, ... strk); (where k is a variable)

I was looking through the java documentation, but could not find a way to do this. Is there a simple way out?

Thanks!

3
  • 6
    Perhaps Arrays.toString() is probably what you are looking for. Commented Aug 20, 2013 at 13:00
  • Not really, the point is that I want to specify the number of characters that I want for each string. Something like %15s Commented Aug 20, 2013 at 13:03
  • 1
    @ashu: your explanation isn't making a lot of sense to me. Could you edit your post an give an example array + output? Commented Aug 20, 2013 at 13:05

9 Answers 9

64

you can use

System.out.format("%s". Arrays.toString(your_array));
Sign up to request clarification or add additional context in comments.

1 Comment

Wont this have the [ and ] contained within the string which we do not want?
10

Java 8:

String formatted = Stream.of(arrayOfStrings)
    .collect(Collectors.joining(",","[","]"));

String formatted = Stream.of(arrayOfSomethingElse)
    .map(Object::toString)
    .collect(Collectors.joining(",","[","]"));

Comments

3

Use a loop:

for (String s : array) {
    System.out.print(String.format("%s ", s));
}
System.out.println();

Comments

3

You can use String.join() to format your array to your desired result.

System.out.println(String.join(" ", array));

1 Comment

This is the most efficient actually
2
for(int i = 0; i < array.length; i++) {
    System.out.print("%s ", array[i]);
}

2 Comments

You'd say array[i]. I also would suggest use array.length instead k
I whipped that together too quick, thanks for pointing that out.
1

Try:

StringBuilder sb = new StringBuilder();
for(String s : myArray){
    sb.append(s).append(" ");
}
sb.append(myArray.length).append(" times");
System.out.println(sb.toString());              // print the string 

Comments

1

Do you simply want to concatenate k strings with a space between each of the strings? You don't need System.out.format for that. You could simply create a loop to concatenate them together with a StringBuilder:

public String concatStrings(String... s) {
    StringBuilder sb = new StringBuilder();
    if (s.length > 0) {
        sb.append(s[0]);
        for (int i = 1; i < s.length; i++) {
            sb.append(' ').append(s[i]);
        }
    }

    return sb.toString();
}

Comments

1

I want to specify the number of characters that I want for each string. Something like %15s

That will only specify the padding for each String. If the String length is less than the value specified in the format specifier, then the full String is used. You could use substring

void displayArray(String[] str, int characters) {
    for (String s: str) {
        System.out.print(s.substring(0, Math.min(s.length(), characters)) + " ");
    }
    System.out.println();
}

Comments

1

That's typical toolbox code - code you often use and reuse, and best keep in a static-access utility class (such as StringUtil). Here's a generic function that works on all kinds of non-primitive arrays, and lets you specify the separator (space, comma, slash, whatever):

public static <T> void print (PrintStream out, String separator, T... elements) {
        for (int i = 0; i < elements.length; i++) {
            if (i > 0) {
                out.print(separator);
            }
            out.print(elements[i]);
        }
    }

Example Usage:

String[] s = {"A", "B", "C", "D", "E"};
Integer[] n = {1, 2, 3, 4, 5}; //non-primitive
print(System.out, " ", s);
print(System.out, ", ", n);

Comments

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.