Archive for the ‘Java Program’ Category

Java Program: Getting inputs from the user

November 10, 2010

DOS Based

Using the BufferedReader Class

The BufferedReaderclass is typically  used  in  getting  input  from  the
keyboard.  This class is found in the  package.

Here are the steps in getting input:

1. Import the I/O package:


2. Create a  BufferedReader object from the  object.

BufferedReader dataIn = new BufferedReader(new InputStreamReader(;

3.  Declare a temporary  String  variable to get the input, and invoke the
readLine()  method to get input from the keyboard.

name = dataIn.readLine();


Sample Source Code:


public class Sample {

public static void main (String args[]) throws IOException{

BufferedReader dataIn = new BufferedReader(new InputStreamReader(;
System.out.print("Enter the first input : ");
String name = dataIn.readLine();
System.out.print("Enter the second input : ");
String name2 = dataIn.readLine();
System.out.println("Your inputs are : "+name+" "+name2);




GUI Based

Sample Source Code:
import javax.swing.*;

public class Sample2 {

public static void main (String args[]){

String a = JOptionPane.showInputDialog("Enter:" );

JOptionPane.showMessageDialog(null,"Hello "+a);





Pls visit the following links for more information: