/
Chapter 5 GC 101 Input & Output Chapter 5 GC 101 Input & Output

Chapter 5 GC 101 Input & Output - PowerPoint Presentation

belinda
belinda . @belinda
Follow
65 views
Uploaded On 2023-11-04

Chapter 5 GC 101 Input & Output - PPT Presentation

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 ID: 1028499

system scanner console input scanner system input console println nextint int java age user util class public width string

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Chapter 5 GC 101 Input & Output" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. Chapter 5GC 101Input & Output1

2. Interactive programsWe 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

3. Input and System.inSystem.outAn object with methods named println and printSystem.innot intended to be used directlyWe 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

4. Java class libraries, importJava class libraries: Classes included with Java's JDK.organized into groups named packagesTo 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

5. Scanner methodsEach 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.MethodDescriptionnextInt()reads a token of user input as an intnextDouble()reads a token of user input as a doublenext()reads a token of user input as a StringnextLine()reads a line of user input as a String5

6. Common Scanner MethodsMethod Example 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

7. Example Scanner usageimport java.util.*; // so that I can use Scannerpublic 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? 1414... That's quite old!7

8. Another Scanner exampleimport java.util.*; // so that I can use Scannerpublic 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: 8 6 13The sum is 27The Scanner can read multiple values from one line.8

9. Input tokenstoken: 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

10. Exampleimport 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

11. Outputenter the length2Enter the Width 3 the length is 2the width is 3the area is 611

12. 12Inputimport java.util.*;public class Example2_16{ static Scanner console = new Scanner(System.in); public static void main(String[] args) { int feet; int inches; System.out.println("Enter two integers separated by spaces."); feet = console.nextInt(); // reads int inches = console.nextInt(); // reads int System.out.println("Feet = " + feet); System.out.println("Inches = " + inches); }}Required to use the class Scanner

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

14. 14Inputimport java.util.*;public class Example2_17{ static Scanner console = new Scanner(System.in); public static void main(String[] args) { String firstName; String lastName; int age; double weight; System.out.println("Enter first name, last name, " +"age, and weight separated by spaces."); firstName = console.next(); lastName = console.next(); age = console.nextInt(); weight = console.nextDouble(); System.out.println("Name: " + firstName + " " + lastName); System.out.println("Age: " + age); System.out.println("Weight: " + weight); }}

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