Java Command Line Arguments Example


Write a Java application which takes several command line arguments, which are supposed to
be names of students and prints output as given below:
(Suppose we enter 3 names then output should be as follows)..
Number of arguments = 3
1.:
First Student Name is =
Tom
2.:
Second Student Name is =
Dick
3.:
Third Student Name is =
Harry
Hint: An array may be used for converting from numeric values from 1 to 20 into String.

Program:

public class q3
{
  public static void main(String args[])
  {
    String arr[] = {"First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eigth", "Ninth", "Tenth"};
    int num = args.length, j=1;
    System.out.println("Number of arguments: "+num);
    for(int i=0; i<num; i++)
    {
        System.out.println(j+". "+arr[i]+" Student name is: "+args[i]);
        j++;
    }
  }
}

Comments

Popular Posts