Java Program: Getting inputs from the user

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

Tags: , ,

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s


%d bloggers like this: