-1

I wrote this

public class Main {
    public static void main(String[] args) {
        for (int i =0; i < args.length; i++){
            System.out.println(args[i]);
        }
    }
}

in cmd typed:

C:\> javac Main.java

C:\> java Main first second third

The console showed me

first
second
third

Questin is how to make that cmd shows me arguments in one line?

4 Answers4

3

Should be something like this.

public class Main {
    public static void main(String[] args) {
        for (int i =0; i < args.length; i++){
            System.out.print(args[i] + " ");
        }
    }
}
Raymond Nijland
  • 11,488
  • 2
  • 22
  • 34
3

You are using System.out.println().

The println method will terminate current line by writing the line separator string for you. (Which in your case is the newline character.)

Instead, you want to use System.out.print().

Like so:

System.out.print(args[i] + " ");

This will print your argument, followed by a space instead of a newline character.

Zsw
  • 3,920
  • 4
  • 29
  • 43
1

Use System.out.print() instead of println().

Syam Danda
  • 587
  • 3
  • 12
  • 34
1

If you want the printing to continue in the same line, you need to invoke a print() method instead of a println() method.

Also, since this resembles the echo program, if you want it to be perfect, you want to have space between the strings, but not before the first or after the last.

With Java 4 and older:

public class Main {
    public static void main(final String[] args) {
        for (int i = 0; i < args.length; i++) {
            if (i >  0)
                 System.out.print(" ");
            System.out.print(args[i]);
        }
        System.out.println();
    }
}

With Java 5-7 you could use foreach-loops, although in this special case that's not really better, it's just for completeness:

public class Main {
    public static void main(final String... args) {
        boolean isFirst = true;
        for (final String arg : args) {
            if (isFirst)
                isFirst = false;
            else
                System.out.print(" ");
            System.out.print(arg);
        }
        System.out.println();
    }
}

With Java 8:

public class Main {
    public static void main(final String... args) {
        System.out.println(String.join(" ", args));
    }
}

If you want your program to be fast - which would not be necessary in this case, this again is just for completeness - you would want to first assemble the String and then print it in one go.

With Java 4 and older:

public class Main {
    public static void main(final String[] args) {
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < args.length; i++) {
            if (i > 0)
                sb.append(' ');
            sb.append(args[i]);
        }
        System.out.println(sb);
    }
}

With Java 5-7:

public class Main {
    public static void main(final String... args) {
        final StringBuilder sb = new StringBuilder();
        for (final String arg : args)
            sb.append(arg);
        if (args.length > 0)
            sb.setLength(sb.getLength() - 1); // skip last space
        System.out.println(sb);
    }
}

With Java 8 (no difference):

public class Main {
    public static void main(final String... args) {
        System.out.println(String.join(" ", args));
    }
}

The code which I prefer:

import static java.lang.String.join;
import static java.lang.System.out;

public class Main {
    public static void main(final String... args) {
        out.println(join(" ", args));
    }
}

Hope this helps, have fun with Java!

Christian Hujer
  • 17,035
  • 5
  • 40
  • 47