Page 1 of 1

Command-line Arguments Tutorial Rate Topic: -----

#1 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Posted 17 September 2009 - 12:30 PM

Hi there! I'm Anton, and I'll be teaching all of you readers about Command-Line Arguments.

Now, every time you start to code in Java, there MUST be a main method, right? Have you ever wondered what does the "String[] args" parameter do?

public static void main(String[] args){
	// Codes here...
}



The main method is just like a regular method with a parameter, so of course it's possible to pass arguments to main method.

Assuming that the name of the program is JavaExample, this is what Java coders type to run their programs via command prompt(Windows) or terminal(Linux):

java JavaExample



Consider this example:
java JavaExample "I Love DIC" Hola 500



In this example, three strings are passed to the main method when the program runs.
Don't be fooled by the "500"...it's not an integer, it's actually treated as String.
Since all the args entered are considered as Strings, there is no need to use double-quotes, like what I did with I Love DIC.

I am going to show you how it works in a complete program now.


public class TestCommandLineArgs {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length != 3){
			System.err.println("Usage: java TestCommandLineArgs numbers1 numbers2 numbers3");
			System.exit(0);
		}
		
		int num1 = Integer.parseInt(args[0]);
		int num2 = Integer.parseInt(args[1]);
		int num3 = Integer.parseInt(args[2]);
		
		int sum = num1 + num2 + num3;
		System.out.println("The sum of the three numbers are " + sum);
		
	}

}



This program is used to sum up all three numbers that are entered as args.

The if(args.length != 3) statement is used to ensure that the user has entered 3 arguments. The program terminates if the user enters less than or more than 3 arguments.

Since the args entered are of String type, we need Integer.parseInt(args[0]) to convert them to integer.
Then calculation's made, and the sum is printed out.

I hope that my explanations and example on Command-Line arguments are clear enough for all of you guys.
Thanks for reading. =)

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 3
  • +

Replies To: Command-line Arguments Tutorial

#2 NeoTifa  Icon User is online

  • Whorediot
  • member icon





Reputation: 2668
  • View blog
  • Posts: 15,706
  • Joined: 24-September 08

Posted 05 October 2009 - 09:37 AM

C:/Hacking Tools? >_> very nice though.
Was This Post Helpful? 0
  • +
  • -

#3 waltf  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 10
  • Joined: 23-December 09

Posted 28 December 2009 - 10:32 AM

View PostAntonWebsters, on 17 Sep, 2009 - 11:30 AM, said:

...
Consider this example:
java JavaExample "I Love DIC" Hola 500



In this example, three strings are passed to the main method when the program runs.
Don't be fooled by the "500"...it's not an integer, it's actually treated as String.
Since all the args entered are considered as Strings, there is no need to use double-quotes, like what I did with I Love DIC.


Just to clarify a little something for beginners, in reference to your statement, "Since all the args entered are considered as Strings, there is no need to use double-quotes, like what I did with I Love DIC.":

A beginner could misconstrue your statement to mean you don't need the double-quotes around the "I Love DIC" and not that you don't need the double-quote around "500" for it to be passed into the program as a String....

Maybe better written like this:

Quote

In this example, three strings are passed to the main method when the program runs.
Don't be fooled by the "500"...it's not an integer, it's actually treated as String.
Since all the args entered are considered as Strings, there is no need to use double-quotes around an argument in order for it to be passed into the program as a String. Note that you still need the double-quotes around the argument "I Love DIC" in order for it to be passed in as a single argument.



:D
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1