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:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: