1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.

Slides:



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

Lecture 15: I/O and Parsing
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
©2004 Brooks/Cole Chapter 3 Interactive Input. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Standard Input and Output System.in Is Used to.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
String Tokenization What is String Tokenization?
Java Review Rem Collier. A Statement I have been asked to do these Lectures at YOUR request. They are to help YOU with any Java question that you have.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
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 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.
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.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
Static Class Members Wrapper Classes Autoboxing Unboxing.
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.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Java I/O Input: information brought to program from an external source
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.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
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.
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.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Lecture 2 Objectives Learn about objects and reference variables.
Lecture 5 I/O and Parsing
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
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.
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.
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.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Java Programming. Objects  Java is an object-oriented programming language –use objects to define both the data type and the operations that can be applied.
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.
Input Characters from the Keyboard tMyn1 Input Characters from the Keyboard We have been using console output, but not console (keyboard) input. The main.
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.:
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Objectives You should be able to describe: Interactive Keyboard Input
Java Review Rem Collier.
Strings and File I/O.
Interactive Standard Input/output
Objects, Classes, Program Constructs
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Unit-2 Objects and Classes
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
Chapter 8: Exceptions and I/O Streams
Presentation transcript:

1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered Reader Object  Reading Strings  Numeric Input

2 Primitive Type Wrapper Classes  Java uses primitive types, such as int and char, for performance reasons.  However, there are times when a programmer needs to create an object representation for one of these primitive types.  Java provides a Wrapper class for each primitive type. All these classes are in the java.lang package: Primitive typeWrapper class charCharacter doubleDouble floatFloat intInteger longLong   Wrapper classes are used to provide constants and general methods for the primitive data types.

3 String to Number Conversion  Each of the Wrapper classes Double, Float, Integer, and Long has a method to convert the string representation of a number of the corresponding primitive type into its numeric format: Wrapper classparse method DoubleparseDouble(string) FloatparseFloat(string) IntegerparseInt(string) LongparseLong(string)   Examples: int numStudents = Integer.parseInt(“500”); String inputLine = “3.5”; double studentGPA = Double.parseDouble(inputLine);

4 System.in stream  In Java I/O is handled by streams.  An input stream is an object that takes data from an input source and delivers that data to a program.  An output stream is an object that takes data from a program and delivers it to an output destination. [ e.g., System.out that corresponds to the monitor]  In Java, console input is usually accomplished by reading from the input stream System.in of the class java.lang.System  System.in represents the standard input stream (i.e., it corresponds to the keyboard).  System.in has no methods for reading characters, strings, or numbers. It has a read method to read a single byte at a time. [Java uses Unicode in which each character is two bytes].

5 Wrapping System.in in a BufferedReader Object  To be able to read characters, strings, or numbers, System.in must be wrapped in other objects.  To turn System.in into a Reader object (i.e., an object that is capable of reading one character at a time), wrap it in an InputstreamReader object: InputStreamReader reader = new InputStreamReader(System.in);  To turn the object referenced by reader into an object with the ability to read entire lines at a time, wrap the object in a Buffered Reader object: BufferedReader stdin = new BufferedReader(reader);  The steps of turning System.in into a Buffered Reader object can be combined into a single statement: BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));  Note: Both the Buffered Reader class and the InputstreamReader class are defined in the java.io package.

6 Reading Strings  A Buffered Reader object contains a readLine method that reads one input line at a time, and returns that line as a string.  The reading of input may cause an error. Input errors are called lOExceptions.  A programmer may write code to handle lOExceptions or he must declare that lOExceptions are not handled by a method by using the clause: throws lOException in the method header.  Example: import java.io.*; public class ReadString{ public static void main(String[] args)throws IOException{ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter a line of text:”); String message = stdin.readLine(); System.out.println(“You entered: “ + message); }

7 Numeric Input  The Java library contains no classes to read numbers directly.  One way of processing numeric input is to read it as a string using the readLine method.  The string is then converted to its corresponding numeric value using the parse method of an appropriate Wrapper class.  Examples: String inputLine = stdin.readLine(); int numStudents = Integer.parseInt(inputLine); double speed = Double.parseDouble(stdin.readLine()); floatHeight = Float.parseFloat(stdin.readLine().trim()); import java.io.*; public class ReadIntegers{ public static void main(String[] args)throws IOException{ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println(“Enter two integers on separate lines:”); int num1 = Integer.parseInt(stdin.readLine()); int num2 = Integer.parseInt(stdin.readLine()); System.out.println(“Sum = “ + (num1 + num2)); }