I have discussed about how to take input from user in runtime from command line here.
Here I will try to give a simple example regarding this.
In this example, our program will take total student number and total subject as input, then it will take input of each subject number. After calculating the average number of total subject, if any student gets less than 40 it will show that student is failed and if any student gets more than 40 that student is passed.
Here is the given Code:
import java.io.*;
class student
{
public static void main(String args[])
{
String inputa;
int num_std; // Total Number of Student
int sub_std; // Total Subject of Student
System.out.println(“************************************”);
System.out.println(“\n”);
System.out.println(“Enter the Total Number of Students:”);
System.out.println(“\n”);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
inputa=br.readLine();
num_std=Integer.parseInt(inputa);
System.out.println(“Enter the Total Subject of Student:”);
System.out.println(“\n”);
inputa=br.readLine();
sub_std= Integer.parseInt(inputa);
int i=0;
int j=0;
int z=0;
System.out.println(“\n”);
for (i=1;i<=num_std;i++)
{
System.out.println(“Enter Student :” +i+” :Subject Number:”);
System.out.println(“\n”);
for (j=1;j<=sub_std;j++)
{
inputa= br.readLine();
z= z+Integer.parseInt(inputa);
if(j==sub_std)
{
System.out.println(“Student:”+i+”Total Number:”+z);
int om=(z)/sub_std;
if(om<40)
{
System.out.println(“Student:”+i+”fails”);
System.out.println(“Student :”+i+” : Average Number:”+om);
om=0;
z=0;
}
else
{
System.out.println(“Student :”+i+” : passed”);
System.out.println(“Student :”+i+” : Average Number:”+om);
om=0;
z=0;
}
}
} //End of student subject loop
} //End of student num loop
System.out.println(“\n”);
System.out.println(“************************************”);
}
catch(IOException e)
{}
}
}
OUTPUT: