1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.

Slides:



Advertisements
Similar presentations
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Scanner & Stepwise Refinement Pepper With credit to Dr. Siegfried.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 Hours question Given a file hours.txt with the following contents: 123 Kim Eric Stef
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.
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Copyright 2010 by Pearson Education Building Java Programs Scanner ; if / else; while loops ; random reading: 3.3 – 3.4, 4.1, 4.5, 5.1, 5.6.
CS 112 Introduction to Programming Summary of Methods; User Input using Scanner Yang (Richard) Yang Computer Science Department Yale University 308A Watson,
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Line-based file processing suggested reading:6.3.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Building Java Programs
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
CSCI 161 – Introduction to Programming I William Killian
Lecture 7: Input and Miscellaneous
Building Java Programs
Introduction to Methods in java
Topic 11 Scanner object, conditional execution
Building Java Programs
INPUT STATEMENTS GC 201.
Introduction to Classes and Methods
Building Java Programs
Scanner objects Readings: 3.4.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Scanner objects Readings: 3.4.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Scanner objects Readings: 3.4.
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Building Java Programs
Building Java Programs
Building Java Programs
Computer Science Club 1st November 2019.
Optional Topic: User Input with Scanner
Presentation transcript:

1 Scanner objects

2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The user types the input into the console.  The program uses the input to do something.  Such a program is called an interactive program.

3 Scanner Constructing a Scanner object to read the console: Scanner = new Scanner(System.in); Example: Scanner console = new Scanner(System.in);

4 Scanner methods Some methods of Scanner : Each of these methods pauses your program until the user types input and presses Enter.  Then the value typed is returned to your program. reads and returns user input as a doublenextDouble()‏ reads and returns user input as an intnextInt()‏ reads and returns user input as a Stringnext()‏ DescriptionMethod

5 Using a Scanner object Example: 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 printed to the user, telling them what input to type.

6 Input tokens token: A unit of user input, as read by the Scanner.  Tokens are separated by whitespace (spaces, tabs, new lines).  How many tokens appear on the following line of input? 23 John Smith 42.0 "Hello world" When the token doesn't match the type the Scanner tries to read, the program crashes.  Example: System.out.print("What is your age? "); int age = console.nextInt(); Sample Run: What is your age? Timmy InputMismatchException: at java.util.Scanner.throwFor(Unknown Source)‏ at java.util.Scanner.next(Unknown Source)‏ at java.util.Scanner.nextInt(Unknown Source)‏...

7 Importing classes Java class libraries: A large set of Java classes available for you to use.  Classes are grouped into packages.  To use the classes from a package, you must include an import declaration at the top of your program.  Scanner is in a package named java.util Import declaration, general syntax: import.*; To use Scanner, put this at the start of your program: import java.util.*;

8 A complete program import java.util.*; // so that I can use Scanner public class ReadSomeInput { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("What is your first name? "); String name = console.next(); System.out.print("And how old are you? "); int age = console.nextInt(); System.out.println(name + " is " + age + ". That's quite old!"); } } Sample Run: What is your first name? Marty How old are you? 12 Marty is 12. That's quite old!

9 Another complete program import java.util.*; // so that I can use Scanner public class Average { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Please type three numbers: "); int num1 = console.nextInt(); int num2 = console.nextInt(); int num3 = console.nextInt(); double average = (num1 + num2 + num3) / 3.0; System.out.println("The average is " + average); } } Sample Run: Please type three numbers: The average is 9.0 Notice that the Scanner can read multiple values from one line.