CHAPTER 5 GC 101 Input Output 1 INTERACTIVE

  • Slides: 15
Download presentation
CHAPTER 5 GC 101 Input & Output 1

CHAPTER 5 GC 101 Input & Output 1

INTERACTIVE PROGRAMS We have written programs that print console output, but it is also

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

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

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 package. Name. *; 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 Method next. Int() Description reads a token of user input as an

SCANNER METHODS Method next. Int() Description reads a token of user input as an int next. Double() reads a token of user input as a double next() reads a token of user input as a String next. Line() reads a line of user input as a String Each method waits until the user presses Enter. The value typed is returned. System. out. print("How old are you? "); int age = console. next. Int(); System. out. println("You'll be 40 in " + (40 - age) + " years. "); // prompt: A message telling the user what input to type. 5

COMMON SCANNER METHODS Method Example Scanner input = new Scanner (System. in); next. Double(

COMMON SCANNER METHODS Method Example Scanner input = new Scanner (System. in); next. Double( ) double d = input. next. Double( ); next. Float( ) float f = input. next. Float( ); next. Int( ) int i = input. next. Int( ); next() String str = input. next(); 6

EXAMPLE SCANNER USAGE import java. util. *; // so that I can use Scanner

EXAMPLE SCANNER USAGE import java. util. *; // so that I can use Scanner public class Read. Some. Input { public static void main(String[] args) { Scanner console = new Scanner(System. in); System. out. print("How old are you? "); int age = console. next. Int(); System. out. println(age + ". . . That's quite old!"); } } Output (user input underlined): How old are you? 14 14. . . That's quite old! 7

ANOTHER SCANNER EXAMPLE import java. util. *; // so that I can use Scanner

ANOTHER SCANNER EXAMPLE import java. util. *; // so that I can use Scanner public class Scanner. Sum { public static void main(String[] args) { Scanner console = new Scanner(System. in); System. out. print("Please type three numbers: "); int num 1 = console. next. Int(); int num 2 = console. next. Int(); int num 3 = console. next. Int(); int sum = num 1 + num 2 + num 3; System. out. println("The sum is " + sum); } } Output (user input underlined): Please type three numbers: 8 6 13 The sum is 27 8 The Scanner can read multiple values from one line.

INPUT TOKENS token: A unit of user input, as read by the Scanner. Tokens

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. next. Int(); Output: What is your age? Timmy java. util. Input. Mismatch. Exception at java. util. Scanner. next(Unknown Source) at java. util. Scanner. next. Int(Unknown Source). . . 9

import java. util. Scanner; public class Test. Input { EXAMPLE public static void main(String[]

import java. util. Scanner; public class Test. Input { EXAMPLE 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. next. Int(); //reading the length from the keyboard System. out. println("Enter the Width "); width = input. next. Int(); //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

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

INPUT 1. import java. util. *; Required to use the class Scanner 2. public

INPUT 1. import java. util. *; Required to use the class Scanner 2. public class Example 2_16 3. { 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. } 12 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. next. Int(); // reads int inches = console. next. Int(); // reads int System. out. println("Feet = " + feet); System. out. println("Inches = " + inches); }

INPUT Enter two integers separated by spaces. > 23 7 Feet = 23 Inches

INPUT Enter two integers separated by spaces. > 23 7 Feet = 23 Inches = 7 If the user enters a non integer number for example 24 w 5 or 3. 4 console. next. Int() will cause a program termination. 13

1. import java. util. *; 2. public class Example 2_17 3. { 4. static

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

Enter first name, last name, age, and weight separated by spaces. > Sheila Mann

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