Java Input and Output - PowerPoint PPT Presentation

About This Presentation
Title:

Java Input and Output

Description:

Java Input and Output. Java Input. Input is any information needed ... Output is any information that the program must ... out.print('Johnny 5 needs more ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 26
Provided by: mccl8
Category:
Tags: input | java | johnny | output

less

Transcript and Presenter's Notes

Title: Java Input and Output


1
  • Java Input and Output

2
Java Input
  • Input is any information needed by your program
    to complete its execution
  • So far we have been using InputBox for this
  • Examples of other Input
  • Direct keyboard input
  • Mouse
  • Network
  • Disk
  • Microphone

3
Java Output
  • Output is any information that the program must
    convey to the user
  • So far we have been using OutputBox and
    System.out.println()
  • Examples of other Output
  • printer
  • Network
  • Disk
  • Speakers

4
Java I/O
  • We have been using the javabook2 package because
    its easier
  • Its easier because it eliminates a lot of errors
  • Ex. NumberFormatException occurs if user enters
    letters when a number is expected
  • getInteger() checks for this
  • It is also easier because it is simple
  • Getting numbers, Strings, etc. is straightforward
  • These actions become much more complex without
    javabook
  • Standard java classes are not as simple because
    they offer added flexibility. Flexibility comes
    at the cost of increasing complexity.

5
Java Console Input
  • Remember Console window is the black window that
    is automatically launched when you run your
    program
  • We havent used this to get input so far
  • There are three parts to getting console input
  • Prompt the user
  • Get this input
  • Convert the input

6
Java Console Input
  • Prompt the user
  • The user must be told to enter information
  • The user will only know what type of information
    to enter if you tell them

7
Java Console Input
  • Get the input
  • Several standard classes are helpful
  • java.io.InputStream Stores information about
    the connection between an input device and your
    program
  • java.io.InputStreamReader used to translate
    data bytes received from InputStream objects into
    a stream of characters
  • Java.io.BufferedReader used to buffer (store)
    input received from an InputStreamReader object
    (stores input as strings).

8
Java Console Input
  • None of these classes has a method as simple as
    getString
  • BufferedReader has readLine() which returns a
    string
  • So lets make a BufferedReader
  • Look at the constructor online
  • We need a Reader object

9
Java Console Input
  • InputStreamReader extends
  • the Reader class
  • Extends means
  • Robin extends bird
  • Robin is a bird,
  • bird is not necessarily a robin
  • If you need a bird, a robin will do
  • We will use an InputStreamReader for our reader
    (Because we want an InputStream)
  • Look at constructor for InputStreamReader
  • We need an InputStream object

10
Java Console Input
  • The System class in the java.lang package (which
    is automatically imported) automatically creates
    an InputStream object for us called in
  • We can access it with System.in
  • This InputStream is connected to the keyboard
  • We can use it to create our InputStreamReader
    object, which we can use to create our
    BufferedReader object

11
Java Console Input
  • Put it all together
  • 1. Use System.in to create an InputStreamReader
    object
  • 2. Use the InputStreamReader object to create a
    BufferedReader object
  • 3. Display a prompt to the user for the desired
    data
  • 4. Use the BufferedReader object to read a line
    of text from the user
  • 5. Convert/use the input received

12
Java Console Input
  • // 1. Use System.in to create an
    InputStreamReader object
  • InputStreamReader isr new InputStreamReader(Syst
    em.in)
  • // 2. Use the InputStreamReader object to create
    a
  • // BufferedReader object
  • BufferedReader stdin new BufferedReader(isr)
  • // 3. Display a prompt to the user for the
    desired data
  • System.out.print(Johnny 5 needs more input)
  • // 4. Use the BufferedReader object to read a
    line of text from the user
  • String input stdin.readLine()
  • // 5. Convert/use the input received
  • System.out.println(You typed input)

13
Java Console Input
  • Questions??

14
Java Console Input
  • // 1. Use System.in to create an
    InputStreamReader object
  • InputStreamReader isr new InputStreamReader(Syst
    em.in)
  • // 2. Use the InputStreamReader object to create
    a
  • // BufferedReader object
  • BufferedReader stdin new BufferedReader(isr)
  • // 3. Display a prompt to the user for the
    desired data
  • System.out.print(Johnny 5 needs more input)
  • // 4. Use the BufferedReader object to read a
    line of text from the user
  • String input stdin.readLine()
  • // 5. Convert/use the input received
  • System.out.println(You typed input)

15
Java Console Input
  • Dont forget to import java.io.
  • Shorter version
  • BufferedReader stdin new BufferedReader( new
    InputStreamReader (System.in) )

16
Java Console Input
  • Converting input is sometimes necessary
  • String input stdin.readLine()
  • // user enters 123
  • int number Integer.parseInt(input)
  • This can cause Exceptions
  • Look at the Integer class online

17
Java Console Input
  • Questoins??

18
Java Console Output
  • We have used System.out.println() as an
    alternative to OutbutBox and Message box of
    javabook2
  • This console output displays a String of
    characters
  • System.out is an instance of the PrintStream class

19
Java Console Output
  • A Stream object is used to store information
    needed to connect a computer program to an input
    or output device
  • Just like a Reader object adds functionality to
    an InputStream, a Printer object adds
    functionality to an OutputStream
  • Console output is easy in java because printer
    methods (print and println) can handle many types
    of input

20
Java Console Output
  • int x 3
  • char a a
  • boolean r true
  • String phrase Cats meow
  • System.out.println(x)
  • System.out.println(a)
  • System.out.println(r)
  • System.out.println(phrase)

21
java.lang.System
  • java.lang.System automatically creates three
    streams for your program
  • System.in - InputStream
  • System.out - PrintStream
  • System.err - PrintStream

22
Java Console Output
  • Questions????

23
Printing objects
  • In java, you can print anything
  • Wanderer w new Wanderer(Gil,Color.green)
  • System.out.println(w)
  • This prints
  • Wanderer_at_13fac
  • This is a memory location (it is not very useful)

24
Printing objects
  • In java, all objects have a toString( ) method
  • It is inherited from the Object object
  • You can override it by writing your own toString(
    ) for your class
  • public String toString( )
  • String coords ( myLoc.getX() ,
    myLoc.getY() )
  • return myName is at coords

25
Printing objects
  • Now,
  • Wanderer w new Wanderer(Gil,Color.green)
  • System.out.println(w)
  • Will print
  • Gil is at (11,9)
  • This is Useful!
  • It can help you display output easily and debug
    your program
Write a Comment
User Comments (0)
About PowerShow.com