Command Line Argument Help
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] + " ");
}
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....
Code :
[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.
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.
Code :
for (int i = indexVar; i < args.length; i++)
System.out.print(args[i] + " ");
Re: Command Line Argument Help