Sunday, February 22, 2009

Keyboard input

Getting input from keyboard

presentation

Pdf:

Ojbectives

create an intereactive Java program that gets input from the keyboard

use the BufferedReader class to get input from the keyboard using a console

Use the JOptionPane class to get input from the keyboard
using a graphical user interface

* BufferedReader >>from keyboard >> Console
* JOptionPane >>from keybaord >> GUI

Getting input form the keyboard

Two methods of getting input
*BufferedReader class
*JOptionPane class


Using BufferedReader Class

BufferedReader class

-found inthe java.io package
-Used to get input


Steps to get input

1. add this at thetop of y our code:
import java.io.*;

2. add this statement:

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



Steps to get input

3. Declare a temporay String variable to get the input , and
invode the readLin() method to get in put from the keyboard.
You have to type it insed a try-catch block.

try{
String temp = dataIn.readLine();
}catch(IOException e){
System.out.println("Error in getting imput");
}


Sample program

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;


public class GetInputFromKeyboard{

public static void main(String [] args){

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

String name ="";

System.out.print("Please Enter your Name;");
try{
name = dataIn.readLine();
}catch(IOException e){
System.out.println("Error!");
}
System.out.println("Hello" + name +"!");

}
}


Sample Program

The lines,

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;



indicate that we want to use the classes


BufferedReader,

InputStreamReader,

IOException


>>>

java.io package



.These statements can also be written as,

import java.io.*;




The Java Application Programming interface (API) contains
hundreds of predefined classes that youc an use in yor

programs. These classes are organized into what we call packages


Packages contains classes that have related purpose..




the statement,


public class GetInputFromKeyboard{



//means we delcare a class named GetInputFromKeyboard


//The next statement declares the main method

// public static void min (String [] args){



The statments,

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


declares a variable named dataIn,

with the class type BufferedReader.


delcares a String type variable name;;


The next statment,

systme.out.print("Please Enter Your name:");

output a String " please Enter your name:" on the screen


assures that the posssible exceptions that could occurs in the statement


name = dataIn.readLine();


will be caught

will cover more about exception handling in the latter part of this course..



name = dataIn.readLine();


the method call, dataln.readLine().


This value will then be saved to our name variable.

which we will use in our final statement to greet the user.



System.out.println("Hello" + name + "!");



using JOptionPane class


another way to get input from the user is by using the

JOptionPane class which is found in the

javax.swing package..



JOpionPane makes it easy to pop up a standard dialog box that prompts users
for a value or informs them of somethong.


Sample Program





import javax.swing.JOptionPane;

public class GetInputFromKeyboard{

public static void main (String [] args){

String name ="";

name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello" + name + "!";
JOptionPane.showMessageDialog(null,msg);


}

}




Sameple program


The statmement


import javax.swing.JOptionPane;


indicate that we want to import the class JOptionPane from

the javax.swing package.

This can also written as,

import javax.swing.*;



name =JOptionPane.showInputDialog("please enter your name");



create a JOptionPane input dialog, which will display a
dialog with a message, a textfield and an OK button as shown in the figure.

This returns a String which we will save in the name

varibale..



input


please enter your name

{ ]

ok cancel



JOptionPane.showMessageDialog(null,msg);



BufferedReader

JOptionPane..




Discuss


Java has already provide the class the make keyboard input into the program.

for the command line

BufferedReader is using

JOptionPane is for the window program...

No comments:

Post a Comment