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  java.io  package.

Here are the steps in getting input:

1. Import the I/O package:

import java.io.*;

2. Create a  BufferedReader object from the  System.in  object.

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

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:

import java.io.*;

public class Sample {

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

BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
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);

System.exit(0);

}
}

 

 

Pls visit the following links for more information:

http://leepoint.net/notes-java/GUI/containers/20dialogs/10joptionpane.html
http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html

Advertisements