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.

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 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Chapter 2: Java Fundamentals Input and Output statements.
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 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.
LAB 10.
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
Chapter 2-3: Basic Elements of Java. Chapter Objectives Discover how to use arithmetic operators. Examine how a program evaluates arithmetic expressions.
Computer Programming Lab(4).
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.
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.
Chapter 2: Basic Elements of Java
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.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
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
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
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.
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 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
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.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Introduction to programming in java
Lecture 4 – Scanner & Style
CompSci 230 S Programming Techniques
Building Java Programs
Input/Output.
TemperatureConversion
CSCI 161 – Introduction to Programming I William Killian
Lecture 7: Input and Miscellaneous
Building Java Programs
Topic 11 Scanner object, conditional execution
Building Java Programs
INPUT STATEMENTS GC 201.
Introduction to Classes and Methods
A+ Computer Science INPUT.
Building Java Programs
Scanner objects Readings: 3.4.
Fundamentals 2.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2: Java Fundamentals
A+ Computer Science INPUT.
Object Oriented Programming
Building Java Programs
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Building Java Programs
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
Optional Topic: User Input with Scanner
Presentation transcript:

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 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 AND SYSTEM.IN  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

JAVA CLASS LIBRARIES, IMPORT  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 the very top of your program import packageName.*;  Scanner is in a package named java.util import java.util.*; import java.util.Scanner;  To use Scanner, you must place the above line at the top of your program (before the public class header). 4

SCANNER METHODS  Each method waits until the user presses Enter.  The value typed is returned. 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. MethodDescription nextInt() reads a token of user input as an int nextDouble() reads a token of user input as a double next() reads a token of user input as a String nextLine() reads a line of user input as a String 5

COMMON SCANNER METHODS  MethodExample Scanner input = new Scanner (System.in); nextDouble( )double d = input.nextDouble( ); nextFloat( )float f = input.nextFloat( ); nextInt( )int i = input.nextInt( ); next() String str = input.next(); 6

EXAMPLE SCANNER USAGE 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("How old are you? "); int age = console.nextInt(); System.out.println(age + "... That's quite old!"); }  Output (user input underlined): How old are you? That's quite old! 7

ANOTHER SCANNER EXAMPLE import java.util.*; // so that I can use Scanner public class ScannerSum { 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(); int sum = num1 + num2 + num3; System.out.println("The sum is " + sum); }  Output (user input underlined): Please type three numbers: The sum is 27  The Scanner can read multiple values from one line. 8

INPUT TOKENS  token: A unit of user input, as read by the Scanner.  Tokens are separated by whitespace (spaces, tabs, newlines).  How many tokens appear on the following line of input? 23 John Smith 42.0 "Hello world" $2.50 " 19"  When a token is not the type you ask for, it crashes. System.out.print("What is your age? "); int age = console.nextInt(); Output: What is your age? Timmy java.util.InputMismatchException at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source)... 9

EXAMPLE import java.util.Scanner; public class TestInput { public static void main(String[] args) { Scanner input ; int area,length, width; input = new Scanner (System.in); // creating an instance System.out.println("enter the length "); length = input.nextInt(); //reading the length from the keyboard System.out.println("Enter the Width "); width = input.nextInt(); //reading the width from the keyboard area = length * width ; System.out.println("the length is "+ length); System.out.println("the width is "+ width); System.out.println("the area is "+ area); } 10

OUTPUT enter the length 2 Enter the Width 3 the length is 2 the width is 3 the area is 6 11

12 INPUT 1. import java.util.*; 2. public class Example2_16 3. { 4. static Scanner console = new Scanner(System.in); 5. public static void main(String[] args) 6. { 7. int feet; 8. int inches; 9. System.out.println(" Enter two integers separated by spaces."); 10. feet = console.nextInt(); // reads int 11. inches = console.nextInt(); // reads int 12. System.out.println("Feet = " + feet); 13. System.out.println("Inches = " + inches); 14. } 15. } Required to use the class Scanner

13 INPUT Enter two integers separated by spaces. > 23 7 Feet = 23 Inches = 7 If the user enters a non integer number for example 24w5 or 3.4  console.nextInt() will cause a program termination.

14 INPUT 1. import java.util.*; 2. public class Example2_17 3. { 4. static Scanner console = new Scanner(System.in); 5. public static void main(String[] args) 6. { 7. String firstName; 8. String lastName; 9. int age; 10. double weight; System.out.println("Enter first name, last name, " 13. +"age, and weight separated by spaces."); firstName = console.next(); 16. lastName = console.next(); 17. age = console.nextInt(); 18. weight = console.nextDouble(); System.out.println("Name: " + firstName + " " + lastName); 21. System.out.println("Age: " + age); 22. System.out.println("Weight: " + weight); 23. } 24. }

15 Enter first name, last name, age, and weight separated by spaces. > Sheila Mann Name: Sheila Mann Age: 23 Weight: 120.5