1 / 7

Interactive Input

Interactive Input. We have written programs that print console output, but it is also possible to read input from the console. The user types input into the console. We capture the input and use it in our program. Such a program is called an interactive program .

coye
Download Presentation

Interactive Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Interactive Input • We have written programs that print console output, but it is also possible to read input from the console. • The user types input into the console. We capture the input and use it in our program. • Such a program is called an interactive program. • Interactive programs can be challenging. • Computers and users think in very different ways. • Users misbehave.

  2. Input using the Scanner class • System.out • An object with methods named println and print • System.in • not intended to be used directly • We use a second object, from a class Scanner, to help us. • Constructing a Scanner object to read console input: Scanner name = new Scanner(System.in); • Example: Scanner console = new Scanner(System.in);

  3. Packages and import Statements • Java class libraries: Classes included with Java's JDK. • organized into groups named packages • To use a package, put an import declaration in your program. • Syntax: //put this at top of your program import packageName.*; • Scanner is in a package named java.util import java.util.*; • To use Scanner, you must place the above line at the top of your program (before the public class header).

  4. Scanner Methods Methods from Scanner class: • Each method waits until the user presses Enter. • The value entered by the user is returned by method. • token = value typed by user. Tokens are separated by whitespace (blank spaces, tabs, newlines). System.out.print("How old are you? "); // prompt int age = console.nextInt(); System.out.println("You'll be 40 in " + (40 - age) + " years."); • prompt: A message telling the user what input to type.

  5. Fun Stuff with User Input import java.util.*; // so I can use Scanner public class TwoTokens { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.print(“Enter two integers: “); // prompt int num1 = keyboard.nextInt(); // get first number int num2 = keyboard.nextInt(); // get second number int sum = num1 + num2; // sum ‘em System.out.println(“Sum of “ + num1 + “ and “ + num2 + “ is “ + sum); } }

  6. Sample Run • Output (user’s input is underlined): Enter two integers: 123 Sum of 12 and 3 is 15

  7. Reading Strings Scanner reader = new Scanner(System.in); System.out.print(“What is your first name? “); // prompt String firstName = reader.next(); System.out.print(“Where do you work? “); String employer = reader.nextLine(); System.out.println(“Name: “ + firstName); System.out.println(“Employer: “ + employer); Sample Run: What is your first name?Mary Where do you work?The University of Texas at Austin Name: Mary Employer: The University of Texas at Austin

More Related