Download presentation
Presentation is loading. Please wait.
Published byAshlee Wilkerson Modified over 8 years ago
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.