hey this sounds like a really simple question to me but I can't seem to figure out how to do it!

My code so far is

public class numbers {
    public static void main(String[] args) {
        final int N = (args.length);
            for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                StdOut.print(args[j]); System.out.print('\"');

Say I type in nsh ajs lk
I want the output to be;
"nsh ajs lk"

Any help appreciated

Recommended Answers

All 8 Replies

Probably this is what you're looking for:

public class numbers {
    public static void main(String[] args) {
        final int N = (args.length);
        for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++) {
            // this will give a space after
            // every word, except the last
            if (j < N-1) {
                System.out.print(" ");

Hope this helps... :)

Ahhh that's brilliant! Thank you! That prints the spaces I've been looking for. Is there any way to get the program to output exactly what is entered in the command line by the way? For instance the first line puts all the letters together, then the second line is supposed to enter the command line arguments exactly as written so, lo as df should come up as;
"lo as df"

Any idea? Thank you for your help so far!

Doesn't the above code do that already? Please elaborate...

Sorry just re read my post, it formatted it wrong when I posted

I need it to print out spaces as in as  sd    df
"as  sd    df"

You mean you want to put 2 spaces, right?

If that's the case, then look at my code once again and tell me where you can change it to print 2 spaces?

No sorry, I'm not doing a very good job of explaing this, I know how to change the code to make two spaces, what I want to do is, if I input two letters then two spaces then two letters then three spaces I want the output to reflect the spaces as well as the characters, so the output would be two letters, two spaces two letters three spaces, or whatever, depending on what I input into the command line

So you want to echo the command line?

Well, well... consider this simple code snippet for that (from oracle's official tutorial):

public class Echo {
    public static void main (String[] args) {           
        for (String s: args) {

        // you could have also used 
        // for (int i = 0; i < args.length; i++)
        // then inside, println(args[i])
        // the above is just a handy syntactic sugar
        // to print arrays and collections.

Now output depends on how you give the input:

c:\yourCodeDirectory>java ab cd

c:\yourCodeDirectory>java "ab cd"
ab cd

c:\yourCodeDirectory>java "ab    cd  ef       gh            etc"
ab    cd  ef       gh            etc

So, moral of the story, giving inverted commas ("") around your command line input will treat the entire sequence (till you hit enter) as a single entity.

Ahhh I didn't know that, thank you so much for your help! I can finish my program with this information. Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.