Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to Put Spaces in the Output of Passed Arguments?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to Put Spaces in the Output of Passed Arguments?

    When passing in an argument using the cmd console, such as "java Hello Shook Ones" how would you get the output to contain spaces in between the args?

    Here is some of my code:
    if(args.length <= 0)
         System.out.print("Hello World");
    else if (args.length > 0)			
    {
        System.out.print("Hello ");
        for (int i = 0; i < args.length; i++)
              System.out.print(args[i]);
    }

    What would I add to my for loop to have the output appear as "Hello Shook Ones" instead of " Hello ShookOnes."?

    Thanks in advance.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to Put Spaces in the Output of Passed Arguments?

    In passing them in, use double quotes to surround the arguments to denote that it is one string:

    REM from command prompt
    javac helloworld.class "Shook Ones"

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Put Spaces in the Output of Passed Arguments?

    Well is that the only way to do it? This is part of an instructed assignment (of course) and the directions called for an output of "Hello whatever input" if a user were to pass in "java Hello whatever input". So I am assuming there is something within my code that I need to change so that those args can be passed instead instead of passing with "java Hello "whatever input"" Thanks for responding.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to Put Spaces in the Output of Passed Arguments?

    If your strings are coming in separately, then you could simply use what you have, but print out a space between each word

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

  5. #5
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: How to Put Spaces in the Output of Passed Arguments?

    Just put spaces in explicitly:
    if(args.length <= 0)
         System.out.print("Hello World");
    else if (args.length > 0)			
    {
        System.out.print("Hello");
        for (int i = 0; i < args.length; i++)
              System.out.print(" "+args[i]);
    }
    System.out.println();

    Also, the final line I added will print a newline character at the end. You should probably always do this.

  6. #6
    Junior Member
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Put Spaces in the Output of Passed Arguments?

    Thanks guys that worked fine.

Similar Threads

  1. Multi-Valued Command Line Arguments
    By joey86 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 29th, 2009, 11:19 AM
  2. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM
  3. [SOLVED] loop , passed and failed
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: August 25th, 2009, 07:00 AM
  4. How to Pass unlimited Arguments to a Function
    By neo_2010 in forum Java Programming Tutorials
    Replies: 2
    Last Post: July 8th, 2009, 11:39 AM
  5. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM