Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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.InputStreamReader - used to translate data bytes received from InputStream objects into a stream of characters. java.io.BufferedReader - used to temporarily store (buffer) the input received from a InputStreamReader object. 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. 2. 3. 4. 5. Use the System.in object to create an InputStreamReader object. Use the InputStreamReader object to create a BufferedReader object. Display a prompt to the user for the desired data. Use the BufferedReader object to read a line of text from the user. 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 Imports the Java packages that you need: •InputStream •InputStreamReader •BufferedReader 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 Declare the class 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.*; This ignores input errors; we’ll deal with those later. 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 { Declare a string to hold the input 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 Create a new InputStreamReader { 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 { Create a new BufferedReader 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; We use a InputStreamReader stdin = print, not a new InputStreamReader(System.in); println, so BufferedReader console = that there is no new BufferedReader(stdin); carriage return System.out.print( "What's your name? "); at the end of s = console.readLine(); the line. System.out.println( "Hello, " + s ); } } Prompt the user for input Sample import java.io.*; public class HelloAgainWithInput { public static void main ( String args[] ) throws Exception { We use a String s; readLine. InputStreamReader stdin = There is a new InputStreamReader(System.in); read method BufferedReader console = also that reads new BufferedReader(stdin); a single System.out.print( "What's your name? "); character—not s = console.readLine(); what we want. System.out.println( "Hello, " + s ); } } Read 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 ); } } 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 = 2006 - 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? "); Converts a s = console.readLine(); an integer age = Integer.parseInt( s ); yearBorn = 2006 - age; System.out.println( "You were born in " + yearBorn); } } string to 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 = 2006 - age; System.out.println( "You were born in " + yearBorn); } // can use console in other methods you define here … }