How to Find the Number of Arguments Provided at Runtime in Java?


In Java, one way of passing arguments at runtime is by using the command line or terminal. While retrieving those values of command line arguments, we may need to find the number of arguments provided by the user at runtime which is possible with the help of length property. This article aims to explain the process of passing and getting the number of arguments provided by user with the help of example programs.

Getting the Number of Arguments provided by User at Runtime

Before finding the number of command line arguments, our first step is to make a program that allows a user to pass the arguments at runtime.

String[] args

While writing Java programs we often come across the main() method. The execution of a Java application begins when JVM calls this method. It is used along with a parameter named String[] args that accepts String type arguments. It allows us to pass arguments through the terminal and it stores these arguments in an array of strings. We can say that String[] args is a command line argument.

Example 1

The following example will illustrate how we can pass arguments from the terminal to a Java program.

public class Arg {
   public static void main(String []args){
      // for each loop to print argument taken from terminal
      for(String arg : args) { 
          System.out.println(arg);
      }
   }
}

Output

To compile the code from terminal type command: javac Arg.java
To run the code from terminal type command: java Arg “Your String”

PS D:\Java Programs> java Arg "Hello, You are on Tutorials Point"
Hello, You are on Tutorials Point

Till this point, we understood how we can take arguments from users at runtime. Now, our next step would be finding the number of passed arguments.

As explained earlier, to find the number of arguments passed by user at runtime, we can use the length property of String[] args.

Example 2

This example illustrates the use of length property to get the number of arguments.

public class Arg {
   public static void main(String []args) {
      // for each loop to print argument taken from terminal
      System.out.println("List of arguments passed by user: ");
      for(String arg : args) {  
	     System.out.print(arg);
      }
      System.out.println();
      // to print the length of argument
      System.out.println("Number of arguments passed by user: " + args.length);
   }
}

Output

PS D:\Java Programs> java Arg "Hello! how are you"
List of arguments passed by user: 
Hello! how are you
Number of arguments passed by user: 1

If we enclose the arguments within double quotes, it will be treated as one single argument. Hence, we got the result as 1.

Example 3

In the following example, we will provide the input without the double quotes.

public class Arg {
   public static void main(String []args) {
      // for each loop to print argument taken from terminal
      System.out.println("List of arguments passed by user: ");
      for(String arg : args) {  
	     System.out.println(arg);
      }
      // to print the length of argument
      System.out.println("Number of arguments passed by user: " + args.length);
   }
}

Output

PS D:\Java Programs> java Arg Hello! how are you
List of arguments passed by user: 
Hello!
how
are
you

When we don't use double quotes, the arguments will be treated individually.

Example 4

The following example demonstrates the use of length property in retrieving all the arguments passed through the terminal.

public class Arg {
   public static void main(String []args) {
      // for loop to print argument taken from terminal
      System.out.println("List of arguments passed by user: ");
      for (int i = 0; i < args.length; i++) {
         System.out.println("Argument " + (i + 1) + ": " + args[i]);
      }
   }
}

Output

PS D:\Java Programs> java Arg Hello! how are you
List of arguments passed by user: 
Argument 1: Hello!
Argument 2: how
Argument 3: are
Argument 4: you 

Conclusion

In this article, we have learned how to find the number of arguments provided at runtime in Java using the length property of String[] args. We also discovered the use of String[] args that allow a user to pass arguments from the terminal to main() method.

Updated on: 19-Jul-2023

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements