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.

Slides:



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

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Lecture 15: I/O and Parsing
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
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.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
1 Text File I/O 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.
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.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Course Lectures Available on line:
Two Ways to Store Data in a File Text format Binary format.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
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.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
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.
Chapter 2 Elementary Programming
I/O in Java Dennis Burford
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
File IO Basics By Dan Fleck Coming up: Data Streams.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
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.
1 Input/Output. 2 In Java input and output take place through I/O streams – An I/O stream represents an input source or output destination – Streams support.
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.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
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.
Strings and File I/O. Strings Java String objects are immutable Common methods include: –boolean equalsIgnoreCase(String str) –String toLowerCase() –String.
Input Characters from the Keyboard tMyn1 Input Characters from the Keyboard We have been using console output, but not console (keyboard) input. The main.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.:
Static Members and Methods
Strings and File I/O.
Interactive Standard Input/output
Objects, Classes, Program Constructs
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
CMSC 202 Static Methods.
Unit-2 Objects and Classes
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Classes and Objects Static Methods
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
ECE 122 April 14, 2005.
EEC 484/584 Computer Networks
Presentation transcript:

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 Java classes –Classes need to be imported –We have just covered these concepts Many Java books do not do a good job of covering the concepts of I/O

3 Java I/O Approach –We will use the buffered I/O classes –We will also use the Java wrapper classes We will use these to read strings from the keyboard and then convert the string value entered to the desired data type.

4 Author’s Input Approach Text input method char input; input = (char) System.in.read( ); System.in.read( ) reads a character and returns an integer value (Unicode value) Cast to (char) converts integer value to character value Character value assigned to input

5 Our Input Approach We will use a more complex but more flexible approach to input static BufferedReader keyboard = new BufferedReader(new InputStreamReader (System.in)); String name; name = keyboard.readLine( ); Use BufferedReader methods Result is that strings are read from the keyboard

6 Buffered Reader Hierarchy System.in - inputs integer Unicode values InputStreamReader - takes integer values and converts them to characters BufferedReader - takes the character values and buffers them This gives us the string input

7 Input Example public class InputExample { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[ ] args) throws IOException { String input; input = keyboard.readLine( ); }

8 Problems If all we want to input are strings we are ok However what about other data types? intfloat double For these we use wrapper classes Wrapper classes take strings and primitives as input and allows us to convert to our desired data type They wrap a primitive data type into an object

9 Wrapper Classes & Constructors Wrapper classes BooleanCharacterDoubleFloat ByteShortIntegerLong Constructors public Integer( int value ) public Integer( String s ) public Double( double value ) public Double( String s )

10 Numeric Wrapper Class Methods public byte byteValue( ) public short shortValue( ) public int intValue( ) public long longValue( ) public float floatValue( ) public double doubleValue( ) Each method return the value in the data type implied by the method’s name

11 Wrapper Class Constants Wrapper classes are also a source of information about the primitive data types Constants available Integer.MIN_VALUE Integer.MAX_VALUE Float.MIN_VALUE Float.MAX_VALUE

12 Wrapper Classes public static void main(String[ ] args) throws IOException { int age; age = new Integer(keyboard.readLine( )).intValue( ); float balance; balance = new Float(keyboard.readLine( )).floatValue( ); double radius; radius = new Double(keyboard.readLine( )). doubleValue( ); }

13 Anonymous Class Definitions An anonymous object –A class instance that is not assigned to a reference variable new Integer(keyboard.readLine( )).intValue( ); new Circle( );

14 Our Output Approach For output we will use the following static PrintWriter screen = new PrintWriter(System.out, true); screen.print(“Hello World”); screen.flush( ); screen.println(name);

15 PrintWriter Advantages System.out is a print stream In previous implementations of Java the print streams have changed This subjects your programs to possible changes from one release to another PrintWriter class helps to insulate your program from print stream changes

16 FYI Wrapper classes are immutable –Information contained inside a wrapper class can not change