Skip to content
Advertisement

Formatting a string array in java

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!

Advertisement

Answer

Use a loop:

for (String s : array) {
    System.out.print(String.format("%s ", s));
}
System.out.println();
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement