jedi- how to get input in java

Upload: muhammed-hussain

Post on 03-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Jedi- how to get input in java

    1/22

    Introduction to Programming 1 1

    5 Getting Input fromKeyboard

  • 8/12/2019 Jedi- how to get input in java

    2/22

    Introduction to Programming 1 2

    Objectives

    At the end of the lesson, the student should be able to:

    Create an interactive Java program that gets input from the

    keyboard Use the BufferedReader class to get input from the

    keyboard using a console

    Use the JOptionane class to get input from the keyboard

    using a graphical user interface

  • 8/12/2019 Jedi- how to get input in java

    3/22

    Introduction to Programming 1 3

    Getting Input from the

    Keyboard !"o methods of getting input:

    BufferedReader class

    JOptionane class

    graphical user interface

  • 8/12/2019 Jedi- how to get input in java

    4/22

    Introduction to Programming 1 4

    Using BufferedReader Class

    BufferedReader class

    #ound in the $ava%io package

    Used to get input

  • 8/12/2019 Jedi- how to get input in java

    5/22

    Introduction to Programming 1 5

    teps to get Input&% Add this at the top of your code:

    import java.io.*;

    '% Add this statement:

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

  • 8/12/2019 Jedi- how to get input in java

    6/22

    Introduction to Programming 1 6

    teps to get Input(% )eclare a temporary *tring variable to get the input, and

    invoke the read+ine- method to get input from thekeyboard% .ou have to type it inside a try/catch block%

    try{

    String temp = dataIn.readLine();

    }catch( IOException e ){System.out.println(Error in getting input);

    }

  • 8/12/2019 Jedi- how to get input in java

    7/22

    Introduction to Programming 1 7

    ample !rogram1 import java.io.BufferedReader;

    2 import java.io.InputStreamReader;

    3 import java.io.IOException;

    4

    5 public class GetInputFromKeyboard {

    6

    7 public static void main( String[] args ){

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

    10

    11 String name = "";

    12 System.out.print("Please Enter Your Name:");

    13 try{

    14 name = dataIn.readLine();

    15 }catch( IOException e ){16 System.out.println("Error!");

    17 }

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

    19 }

    20 }

  • 8/12/2019 Jedi- how to get input in java

    8/22

    Introduction to Programming 1 8

    ample !rogram

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

    import java.io.IOException;

    !he lines,

    indicate that "e "ant to use the classes BufferedReader,0nput*treamReaderand 0O12ception"hich are inside the$ava%iopackage%

    !hese statements can also be "ritten as,

    import java.io.*;

  • 8/12/2019 Jedi- how to get input in java

    9/22

    Introduction to Programming 1 9

    ample !rogram

    !he Java Application rogramming 0nterface A0- containshundreds of predefined classes that you can use in yourprograms% !hese classes are organi3ed into "hat "e callpackages%

    ackagescontain classes that have related purpose%

  • 8/12/2019 Jedi- how to get input in java

    10/22

    Introduction to Programming 1 10

    ample !rogram

    public class GetInputFromKeyboard {

    public static void main( String[] args ){

    !he statement,

    means "e declare a class named 4et0nput#rom5eyboard

    !he ne2t statement declares the main method%

  • 8/12/2019 Jedi- how to get input in java

    11/22

    Introduction to Programming 1 11

    ample !rogram

    BufferedReader dataIn = new BufferedReader(new InputStreamReader

    ( System.in) );

    !he statement,

    declares a variable named dataIn, "ith the class typeBufferedReader%

    )on6t "orry about "hat the synta2 means for no"% 7e "ill cover more aboutclasses and declaring classes later in the course%

  • 8/12/2019 Jedi- how to get input in java

    12/22

    Introduction to Programming 1 12

    ample !rogram

    String name = "";

    System.out.print("Please Enter Your Name:");

    !he statement,

    declares a Stringvariable "ith identifier name.

    !he ne2t statement,

    outputs a *tring on the screen asking for the user6s name

  • 8/12/2019 Jedi- how to get input in java

    13/22

    Introduction to Programming 1 13

    ample !rogram

    try{

    name = dataIn.readLine();

    }catch( IOException e ){

    System.out.println("Error!");

    }

    !he given block defines a try/catch block%

    !his assures that the possible e2ceptions that could occur inthe statement

    name = dataIn.readLine();

    "ill be catched%

    7e "ill cover more about e2ception handling in the latter part of thiscourse%

  • 8/12/2019 Jedi- how to get input in java

    14/22

    Introduction to Programming 1 14

    ample !rogram

    8o" going back to the statement,

    the method call, data0n%read+ine-, gets input from the user

    and "ill return a *tring value%

    !his value "ill then be saved to our namevariable, "hich "e"ill use in our final statement to greet the user,

    name = dataIn.readLine();

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

  • 8/12/2019 Jedi- how to get input in java

    15/22

    Introduction to Programming 1 15

    Using "option!ane Class

    Another "ay to get input from the user is by using theJOptionane class "hich is found in the $ava2%s"ingpackage%

    JOptionane makes it easy to pop up a standard dialog bo2that prompts users for a value or informs them of something%

  • 8/12/2019 Jedi- how to get input in java

    16/22

    Introduction to Programming 1 16

    ample !rogram1 import javax.swing.JOptionPane;

    2

    3 public class GetInputFromKeyboard {

    4

    5 public static void main( String[] args ){

    6 String name = "";

    7 name=JoptionPane.showInputDialog(Please enter your name");

    8 String msg = "Hello " + name + "!";9 JOptionPane.showMessageDialog(null, msg);

    10 }

    11}

  • 8/12/2019 Jedi- how to get input in java

    17/22

    Introduction to Programming 1 17

    ample !rogram Output

  • 8/12/2019 Jedi- how to get input in java

    18/22

    Introduction to Programming 1 18

    ample !rogram

    import javax.swing.JOptionPane;

    !he statement,

    indicates that "e "ant to import the class JOptionane fromthe $ava2%s"ing package%

    !his can also "ritten as,import javax.swing.*;

  • 8/12/2019 Jedi- how to get input in java

    19/22

    Introduction to Programming 1 19

    ample !rogram

    name=JoptionPane.showInputDialog(Please enter your name");

    !he statement,

    creates a JOptionane input dialog, "hich "ill display adialog "ith a message, a te2tfield and an O5 button as

    sho"n in the figure% !his returns a *tring "hich "e "ill save in the name

    variable%

  • 8/12/2019 Jedi- how to get input in java

    20/22

    Introduction to Programming 1 20

    ample !rogram

    String msg = "Hello " + name + "!";

    !he statement,

    creates the "elcome message, "hich "e "ill store in the

    msgvariable%

  • 8/12/2019 Jedi- how to get input in java

    21/22

    Introduction to Programming 1 21

    ample !rogram

    JOptionPane.showMessageDialog(null, msg);

    !he statement,

    displays a dialog "hich contains a message and an O5

    button%

  • 8/12/2019 Jedi- how to get input in java

    22/22

    Introduction to Programming 1 22

    ummary

    )iscussed t"o "ays of getting input from the user by usingthe classes:

    BufferedReader

    JOptionane

    Brief overvie" of packages

    4roups related classes in Java

    Classes inside packages can be used by importing the package