The following code shows an Example of Using Command Line Arguments in Java.
As can be seen, the program uses command line arguments. Also, there is a check on the length of the String array args. When we provide arguments on the command line, these arguments are assigned to this array of strings. So, when the arguments are not exactly two, the program exits. Otherwise, the arguments are retrieved in the variables s1, and s2. Hence, we create the output using these variables.
public class Exercise1
{
public static void main(String[] args) {
String s1, s2;
if(args.length!=2)
{
System.out.println("Wrong Number of Command Line Arguments!\nExiting...");
System.exit(0);
}
s1=args[0];
s2=args[1];
System.out.println(s1+" Teams "+s2);
}
}
Output
F:\Java\JavaFundamentals\LanguageBasics>java Exercise1 Programmingempire Delhi
Programmingempire Team Delhi