Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Console Input

2 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

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

4 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.

5 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.

6 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 ); }

7 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

8 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

9 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.

10 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

11 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

12 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

13 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.

14 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.

15 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

16 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 = 2006 - age; System.out.println( "You were born in " + yearBorn); }

17 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 = 2006 - age; System.out.println( "You were born in " + yearBorn); } Converts a string to an integer

18 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 “*”.

19 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 ) );

20 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 = 2006 - age; System.out.println( "You were born in " + yearBorn); } // can use console in other methods you define here … }


Download ppt "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."

Similar presentations


Ads by Google