-2

I want to write a program that would display my command-line arguments in the console one after another. I found in oracle tutorials that you can surround passed arguments with "" to put them in one line, but how can I do the same with code?
I made something like this but cant keep space between two arguments.

public class Printargs {

    public static void main(String[] args) {
        for(String s: args){
            System.out.print((' ' + s).trim());
        }
    }

}
Alex
  • 13
  • 2

1 Answers1

0

You can join the elements of args[] with space using String#join:

public class Main {
    public static void main(String[] args) {
        System.out.println(String.join(" ", args));
    }
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110