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++)
                StdOut.print(args[i]);
                System.out.println();
            System.out.print('\"');
            for (int j = 0; j < N; j++)
                StdOut.print(args[j]); System.out.print('\"');
                System.out.println();
    }
}

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

Any help appreciated

Dani AI

Generated

What trips a lot of people up here is that the shell (cmd, PowerShell, bash, etc.) splits your command line into tokens before Java ever runs. By the time main(String[] args) is called, sequences of spaces between tokens are gone. Only spaces inside a single token survive, which is why wrapping the input in quotes makes ’s example work and why ’s suggestion is on the right track. If you do not quote, there is no reliable way to recover the exact number of spaces between separate arguments from args[].

If you are on Java 8+, you can simplify your printing without loops:

System.out.println(String.join("", args));                 // nshajslk
System.out.println("\"" + String.join(" ", args) + "\"");  // "nsh ajs lk"

Note: if you run java Numbers "lo as df", the inner multiple spaces are preserved because they are inside one quoted argument; the String.join(" ", args) only inserts a single space between arguments.

If you truly need the exact line as typed (including multiple spaces between words, leading/trailing spaces, or tabs) without forcing users to quote, read a whole line from standard input rather than using args:

java.util.Scanner sc = new java.util.Scanner(System.in);
String line = sc.nextLine();
System.out.println(line);
System.out.println("\"" + line + "\"");

Two small polish tips: avoid mixing StdOut and System.out in the same program to prevent buffering surprises; pick one consistently. And follow Java conventions by naming the class Numbers, not numbers.

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++)
            System.out.print(args[i]);
        System.out.println();
        System.out.print('\"');
        for (int j = 0; j < N; j++) {
            System.out.print(args[j]);
            // this will give a space after
            // every word, except the last
            if (j < N-1) {
                System.out.print(" ");
            }
        }
        System.out.print('\"');
        System.out.println();
    }
}

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;
loasdf
"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
assddf
"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) {
            System.out.println(s);
        }

        // 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
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, networking, learning, and sharing knowledge.