Command line arguments in Java
Now we are discuss, What is the command line arguments, the command line arguments as a arguments that will be entire on the command prompt to the data. Command line arguments are very usefully technique for input data the user.
Let's take a example, we can input the five students records, ensure that we can made mark sheet, will be enter data on the command prompt.
class result
{
public static void main(String args[])
{
int i=0,hindi,english, maths,science, sst, tot;
String name="",div="";
double per=0.0;
while(i<args.length)
{
name = args[i];
hindi=Integer.parseInt(args[i+1]);
english=Integer.parseInt(args[i+2]);
maths=Integer.parseInt(args[i+3]);
sst=Integer.parseInt(args[i+4]);
science=Integer.parseInt(args[i+5]);
tot=hindi+english+maths+sst+science;
per=(tot*100/500);
i=i+6;
if(per>=60)
div="First";
else if(per<60 && per>=50)
div="Second";
else if(per<50 && per>=40)
div="Third";
else
div="Fail";
System.out.println(" Student Name:"+name);
System.out.println("| Hindi | English | Maths | SSt | Science | Total | Per | Div |");
System.out.println("| "+hindi+" | "+english+" | "+maths+" | "+sst+" | "+science+" | "+tot+" |"+per+" |"+div+" |");
System.out.println("-----------------------------------------------------------------------------------------------");
}
}
}
OUTPUT:
C:\Users\admin22.DESKTOP-Q312MFE.000>javac result1.java
C:\Users\admin22.DESKTOP-Q312MFE.000>java result1 viren 76 89 77 98 85 rahul 78 97 74 88 72 shubham 86 71 69 84 92 yash 70 95 63 96 33
Student Name:viren
| Hindi | English | Maths | SSt | Science | Total | Per | Div |
| 76 | 89 | 77 | 98 | 85 | 425 |85.0 |First |
-----------------------------------------------------------------------------------------------
Student Name:rahul
| Hindi | English | Maths | SSt | Science | Total | Per | Div |
| 78 | 97 | 74 | 88 | 72 | 409 |81.0 |First |
-----------------------------------------------------------------------------------------------
Student Name:shubham
| Hindi | English | Maths | SSt | Science | Total | Per | Div |
| 86 | 71 | 69 | 84 | 92 | 402 |80.0 |First |
-----------------------------------------------------------------------------------------------
Student Name:yash
| Hindi | English | Maths | SSt | Science | Total | Per | Div |
| 70 | 95 | 63 | 96 | 33 | 357 |71.0 |First |
-----------------------------------------------------------------------------------------------