Java Basic Problems

Java: RunTime Input from User Example

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:

Java Basic Problems

Java: How to take Run time input from user

Suppose you are doing your coding in java (let you are running your program from command prompt of your desktop) and you need to take input from user in command line or command prompt while  user  run (actually runtime input) your program.

Now I will show you how to take user input from command prompt or command line in java.

Here I have used BufferedReader  and readLine()  to take input from user in command line/ command prompt while the program was running or  simply in run time.

In the example below  br.readLine() takes  input as String, in the next line we have parsed or convert that String into integer.

Code:

import java.io.*;

public class CmdLineInput

{

CmdLineInput()

{

String inputa;

int num_std;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

System.out.println(“Enter the number of Students:”);

inputa=br.readLine();

num_std=Integer.parseInt(inputa);

System.out.println(“Number of Students:”+num_std);

   }

 catch(Exception e){}

   }

public static void main(String args[])

{

CmdLineInput takeinput=new CmdLineInput();

}

}

OUTPUT: