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:

Leave a comment