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.

Slides:



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

Lecture 15: I/O and Parsing
Computer Programming Lab(7).
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
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.
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
Reading Information From the User Making your Programs Interactive.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
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.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
COMP 14 Introduction to Programming Miguel A. Otaduy May 14, 2004.
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.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
1 Course Lectures Available on line:
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
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 5: Preparing Java Programs 1 Chapter 5 Preparing Java Programs.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
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
Chapter 2: Java Fundamentals
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.
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.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Introduction to Programming
String and Scanner CS 21a: Introduction to Computing I First Semester,
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.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
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.
Files and console applications Chapter 17 This chapter explains: What a file is How to read and write to files How to manipulate folder paths and file.
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.
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.
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.:
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Introduction to programming in java
Using the Console.
TemperatureConversion
Interactive Standard Input/output
Objects, Classes, Program Constructs
Lecture Note Set 1 Thursday 12-May-05
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
INPUT STATEMENTS GC 201.
Yong Choi School of Business CSU, Bakersfield
Introduction to Classes and Methods
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
File Input and Output.
EEC 484/584 Computer Networks
Presentation transcript:

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 get input straight from the user. Our topic: Console Input

What is a console? Console input Any input that is entered in the console window Not typed in a dialog box

Programming for input Like printing out, getting input requires calls to Java packages java.io.InputStream - stores information about the connection between an input device and the computer or program. java.io.InputStream java.io.InputStreamReader - used to translate data bytes received from InputStream objects into a stream of characters. java.io.InputStreamReader java.io.BufferedReader - used to temporarily store (buffer) the input received from a InputStreamReader object. java.io.BufferedReader Improves efficiency Input devices are much slower than the computer's processor and buffering the data reduces the number of times the CPU has to interact with the device itself.

Steps for console input 1. Use the System.in object 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. Do something interesting with the input received from the user.

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); }

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Imports the Java packages that you need: InputStream InputStreamReader BufferedReader

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Declare the class

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } This ignores input errors; we’ll deal with those later.

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Declare a string to hold the input

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Create a new InputStreamReader

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Create a new BufferedReader

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Prompt the user for input We use a print, not a println, so that there is no carriage return at the end of the line.

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Read the input We use a readLine. There is a read method also that reads a single character—not what we want.

Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { String s; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); } Do something with the input

Sample 2: Numeric input import java.io.*; public class HelloWithNumbers { public static void main ( String args[] ) throws Exception { String s; int age; int yearBorn; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); System.out.print( "How old are you? "); s = console.readLine(); age = Integer.parseInt( s ); yearBorn = age; System.out.println( "You were born in " + yearBorn); }

Sample 2: Numeric input import java.io.*; public class HelloWithNumbers { public static void main ( String args[] ) throws Exception { String s; int age; int yearBorn; InputStreamReader stdin = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(stdin); System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); System.out.print( "How old are you? "); s = console.readLine(); age = Integer.parseInt( s ); yearBorn = age; System.out.println( "You were born in " + yearBorn); } Converts a string to an integer

Exercise Write a program that will get two integers, m and n, from the user. The program will then display a rectangle with m rows and n columns of made of “*”.

A static console It is likely you will use console input for Java applications with a main method If you need to use it in other methods in the application besides main, have a static BufferedReader variable (called console, for example) and then you may reuse that variable in other methods Instantiate it upon declaration private static BufferedReader console = new BufferedReader( new InputStreamReader ( System.in ) );

Using a static console import java.io.*; public class HelloWithNumbers { private static BufferedReader console = new BufferedReader(new InputStreamReader(System.in)); public static void main ( String args[] ) throws Exception { String s; int age; int yearBorn; System.out.print( "What's your name? "); s = console.readLine(); System.out.println( "Hello, " + s ); System.out.print( "How old are you? "); s = console.readLine(); age = Integer.parseInt( s ); yearBorn = age; System.out.println( "You were born in " + yearBorn); } // can use console in other methods you define here … }