CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.

Slides:



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

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Input/Output Main Memory istream ostream Disk Drive Keyboard Scanner Disk Drive Monitor Printer stream = sequence of bytes.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
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.
CIS 270—Application Development II Chapter 28—Formatted Output.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.2 Getting the Tools and Setting Up the Development.
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.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
Sahar Mosleh California State University San MarcosPage 1 System.out.println for console output System.out is an object that is part of the Java language.
Slides prepared by Rose Williams, Binghamton University Chapter 2 Console Input and Output.
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.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.6 Access Modifiers,
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
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.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 4- Exception and Error Handling Topic 4.1 Errors and Exceptions Produced by Harvey.
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.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
CPRG 215 Introduction to Object-Oriented Programming with Java Module 1-Introduction to Java Topic 1.3 Write Your First Java Program Produced by Harvey.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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 and Output The system console. In the beginning … When computers were relatively expensive and rare, most users interacted with a terminal –CRT.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.:
Introduction to programming in java
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.1 Fundamental Concepts.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Streams & File Input/Output (I/O)
Yanal Alahmad Java Workshop Yanal Alahmad
TMF1414 Introduction to Programming
Interactive Standard Input/output
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.
Java Programming: From Problem Analysis to Program Design, 4e
I/O Streams- Basics Byte Streams and Character Streams
INPUT STATEMENTS GC 201.
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
Introduction to Java Applications
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey Peters, 2008 Copyright SAIT

Please read the following sections in your textbook Core Java, Volume I–Fundamentals, Eighth Edition By Cay S. Horstmann & Gary Cornell Chapter 3 - Fundamental Programming Structures in Java Input and Output CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT

Input/Output Input needed for various reasons –control (commands/instructions) –make decisions –get data to process –etc. CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.1

Input/Output Traditionally Input came from the keyboard (STDIN – standard input) Output went to the “console display” – the monitor attached to the computer (STDOUT – standard output) Error messages also went to the monitor (STDERR – standard error) CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.1

Input/Output Java has 3 system “connections” in the System class called in, out, and err CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.1

Input/Output System.out is used to print output to the screen (terminal) –returns a PrintWriter object which has the write & writeln methods CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.1

Input/Output System.in returns an InputStream connected to the keyboard We can call read methods in the InputStream object, or attach other objects and call their methods byte [] input = new byte[50]; int numBytesRead = System.in.read(input, 0, 50); –Reads 50 bytes starting at offset 0 into Array called “input”, and returns the actual number of bytes that were read CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.1

Input/Output System is in java.lang package PrintWriter and InputStream are in java.io package java.io has classes specialized for numerous kinds of I/O operations To use import java.io.*; CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.1

About java.io The two basic types of streams are: –Input stream –Output stream Node streams read from or write to a device Filter streams use node streams as input or output CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.2

About java.io Check the documentation for the package InputStream OutputStream Reader Writer We’ll look at this in more detail later in the course CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.2

Input Stream Methods The three basic read() methods int read() int read(byte[]) int read(byte[], int, int) Other InputStream methods void close() int available() skip(long) CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.3

Output Stream Methods The three basic write() methods void write(int) void write(byte[]) void write(byte[], int, int) Other OutputStream methods void close() void flush() CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.4

Reading Input Scanner Scanner in = new Scanner(System.in); String name = in.nextLine(); –The Scanner class is in java.util, so… import java.util.*; –we can also use next() nextInt() And more … CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.5

Reading Input Console –Create a Console object Console in = System.console(); String name = in.readLine(); char[] mypass = in.readPassword(“Hi %10s, enter password:”, name); CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.5

Reading Input Console –Check java docs for method structure: char[]readPassword(String fmt, Object... args)readPasswordStringObject “…” means there can me a variable number of args values from args are formatted and embedded in the String named fmt fmt contains embed markers with formatting rules This is also used in methods like printf()  CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.5

printf - Formatted Print prints string with formatting –System.out.printf (format-string, arg1, arg2,… argN); –formats – see page 66 for more % followed by letter -- eg: –%d -- decimal integer –%f -- floating point –%s – string Can also format dates CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.6

printf - Formatted Print printf Example: double gross = 1200; double cost = 500; double net = gross - cost; System.out.printf(“%-10.10s\$%7.2f”, “Gross”, gross); System.out.printf (“%-10.10s\$%7.2f”, “Cost”, cost); System.out.printf (“%-10.10s\$%7.2f”, “Net”, net); % starts format specification - indicates left justification (default is ‘right’) s and f indicate the data type (string & float) the first 10 indicates the minimum length of the string the second 10 indicates the maximum length of the string the 7 indicates the minimum length of the field the 2 indicates the number of decimal digits CPRG 215 Module 2.6- Reading Input with java.io Copyright SAIT Topic 2.6.6