PhysicsTeacher.in

High School Physics + more

Java program – how to access the input arguments

Here is a Java program that helps you to access the input arguments. If you have noticed this statement: public static void main (String args[]) in a java program, and want to know the use of that String args[], then you are at the right place.

Type, compile, and run to understand how to use the feature of input arguments in a Java program.

Java program to to access the input arguments

class ArgumentTest
{
static int length,i;
static String s;
public static void main (String args[])

{
//int length,i;
//String s;
length=args.length;

for (i=0;i<length;i++)
{
s=args[i];
System.out.println(“Argument Number:(“+ i +”) = “+ s) ;
}
}
}

How to provide the input for Input Arguments in Java

When you run this program with the java command, then in the same line you can provide the inputs separated by a space.

In this program, the length of the input argument ( String ) is found first with .length.

Then using a for loop the contents of args[] are fetched one by one.

See also  Perfect Number Java Program - check if a Number is a Perfect Number
Scroll to top
error: physicsTeacher.in