Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

Lecture 15: I/O and Parsing
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Files and Streams. Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts of text.
Lecture 13: Keyboard Input and Text Files Yoni Fridman 7/23/01 7/23/01.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Chapter 3b Standard Input and Output Sample Development.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Io package as Java’s basic I/O system continue’d.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
1 Course Lectures Available on line:
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
I/O in Java Dennis Burford
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
Input Characters from the Keyboard tMyn1 Input Characters from the Keyboard We have been using console output, but not console (keyboard) input. The main.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:
java.io supports console and file I/O
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Objectives You should be able to describe: Interactive Keyboard Input
University of Central Florida COP 3330 Object Oriented Programming
Program Input/Output (I/O)
I/O Streams A stream is a sequence of bytes that flows from a source to a destination In a program, we read information from an input stream and write.
Interactive Standard Input/output
Objects, Classes, Program Constructs
8/30/2018 CPRG 215 Introduction to Object-Oriented Programming with Java Module 5- The java.io Package Topic 5.1 Input and Output Streams, Readers.
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
I/O Streams A stream is a sequence of bytes that flow from a source to a destination In a program, we read information from an input stream and write information.
I/O and Applet from Chapter 12
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
File Input and Output.
Chapter 8: Exceptions and I/O Streams
Presentation transcript:

Java Input and Output

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  …

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  …

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.

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

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

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).

Java Console Input  None of these classes has a method as simple as getString  BufferedReader has readLine() which returns a string  So let’s make a BufferedReader:  Look at the constructor online  We need a Reader object

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

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

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

Java Console Input // 1. Use System.in to create an InputStreamReader object InputStreamReader isr = new InputStreamReader(System.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);

Java Console Input  Questions??

Java Console Input // 1. Use System.in to create an InputStreamReader object InputStreamReader isr = new InputStreamReader(System.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);

Java Console Input Don’t forget to import java.io.*; Shorter version: BufferedReader stdin = new BufferedReader( new InputStreamReader (System.in) );

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

Java Console Input  Questoins??

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

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

Java Console Output  int x = 3;  char a = ‘a’;  boolean r = true;  String phrase = “Cat’s meow”;  System.out.println(x);  System.out.println(a);  System.out.println(r);  System.out.println(phrase);

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

Java Console Output  Questions????

Printing objects  In java, you can print anything: Wanderer w = new Wanderer(“Gil”,Color.green); System.out.println(w) This prints:  This is a memory location (it is not very useful)

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; }

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