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 3 of 3

Thread: Command Line Argument Help

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

    Default Command Line Argument Help

     
    	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();
    (A little background, all I'm doing is replacing "world" if there are command line arguments, with those args)


    How would I, using a variable that has the index of the 2nd passed data argument, with passing in a comm. line arg. such as "java Hello arg1 arg2 arg3 arg4" get the output of "Hello arg3 arg4"?

    Whenever I add in this variable....
    [COLOR="Red"]int indexVar = 2;[/COLOR]
     
    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[[COLOR="Red"]indexVar[/COLOR]] + " ");
     
             }
    	System.out.println()
    I get the output of Hello arg3 arg3 arg3 arg3.
    I am assuming this is happening because the for loop is looping system.out for the args length which is 4. But I don't know how to fix that, and keep getting that unwanted output.

    Can you guys be of some help? Thanks.


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

    Default Re: Command Line Argument Help

    Instead of using indexVar as the index, start counting in your for-loop from indexVar. Also, if this is the method you want, you will probably want to check to see if args.length <= 2 because if you don't, only "hello " will get printed out.

    for (int i = indexVar; i < args.length; i++)
         System.out.print(args[i] + " ");

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    EmSaint (January 28th, 2010)

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

    Default Re: Command Line Argument Help

    Thank you very much.

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. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 08:34 PM
  3. 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
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM