1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: 4.1 - 4.3 Suggested self-checks: Section 4.10 #1-4 These lecture.

Slides:



Advertisements
Similar presentations
BUILDING JAVA PROGRAMS CHAPTER 6.4 FILE OUTPUT. 22 PrintStream : An object in the java.io package that lets you print output to a destination such as.
Advertisements

Copyright 2008 by Pearson Education Line-based file processing reading: 6.3 self-check: #7-11 exercises: #1-4, 8-11.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Topic 20 more file processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
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
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.
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
Topic 19 file input, line based Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 6: File Processing Lecture 6-2: Advanced file input reading: self-check: #7-11.
1 Building Java Programs Chapter 6 Lecture 6-3: Section Problems reading:
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.
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.
FILE PROCESSING. Reading files To read a file, pass a File when constructing a Scanner. Scanner name = new Scanner(new File(" file name ")); Example:
1 CSE 142 Lecture Notes Conditional Execution with if Statements; Methods that Return Values (briefly) Chapters 3 and 4 Suggested reading: ;
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 Reminder: String Scanners A Scanner can be constructed to tokenize a particular String, such as one line of an input file. Scanner = new Scanner( );
File Processing Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from "We can only.
1 Line-based file processing suggested reading:6.3.
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 2008 by Pearson Education Building Java Programs Chapter 6 Lecture 6-3: Searching Files reading: 6.3, 6.5.
1 Line-based file processing reading: 6.3 Use this if you seem to need line based processing for some aspects of your program and token based for others.
Building Java Programs
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
Lecture 7: Input and Miscellaneous
Building Java Programs
Building Java Programs
Topic 9 Using Objects, Interactive Programs and Loop Techniques
Topic 11 Scanner object, conditional execution
Building Java Programs
Topic 20 more file processing
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
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
Building Java Programs
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture notes are copyright (C) Marty Stepp May not be rehosted, copied, sold, or modified without Marty Stepp's expressed written permission. All rights reserved.

2 Interactive programs We have written several programs that print output to the console. It is also possible to read text input from the console. The user running the program types the input into the console. We can capture the input and use it as data in our program. A program that processes input from the user is called an interactive program. Interactive programs can be challenging: Computers and users think in very different ways. Users tend to misbehave!

3 Input and System.in We have now seen code that communicates with objects. Example objects: DrawingPanel, Graphics, Color When we print text output to the console, we communicate with an object named System.out. We call the println (or print ) method of the System.out object to print a message to the console. The object that holds the user's console input is named System.in. But it is not easy to use...

4 Scanner Since System.in is not easy to use by itself, we will use a second object of a type named Scanner to help. Once we construct the Scanner object, we can ask it to read various kinds of input from the console. Constructing a Scanner object to read console input: Scanner = new Scanner(System.in); Example: Scanner console = new Scanner(System.in);

5 Scanner as data source Think of a Scanner like a faucet or showerhead that can be attached to a source of 'water' (data). In our case, the source of that data is System.in. Like a faucet must be connected to a water source, the Scanner must be connected to a data source by writing ( System.in ) when constructing it. The Scanner requires a file named Scanner.java that must be saved into the same folder as your program. Scanner.java is available on the course web page.

6 Scanner methods Methods of Scanner that we will use in this chapter: Each of these methods causes your program to pause until the user has typed input and pressed Enter, then it returns the typed value to your program. (You may wish to review return values from last chapter.) MethodDescription next() reads and returns next token as a String nextDouble() reads and returns next token as a double nextInt() reads and returns next token as an int nextLine() reads and returns next entire line of input as a String

7 Example Scanner usage public class ReadSomeInput { public static void main(String[] args) { System.out.print("How old are you? "); int age; Scanner console = new Scanner(System.in); age = console.nextInt(); System.out.println("Wow, you're " + age); System.out.println("That's quite old!"); } Output (user input underlined): How old are you? 14 Wow, you're 14 That's quite old!

8 Scanning tokens token: A unit of user input. Tokens are separated by whitespace (spaces, tabs, new lines). Example: If the user types the following: John Smith "Hello world" The tokens in the input are the following, and can be interpreted as the given types: TokenType(s) int, double, String double, String 3. John String 4. Smith String 5. "Hello String 6. world" String double, String int, double, String

9 Consuming input When the Scanner's methods are called, the Scanner reads and returns the next input value to our program. If the type of the token isn't compatible with the type we requested, the program crashes. Imagine the Scanner as having an invisible cursor that moves through all the user input. As the scanner reads each input value, it advances forward through the user input until it has passed the given token. This is called consuming the input token. Key: code consumed unconsumed last token double pi = console.nextDouble(); // \t3.14 John Smith\t"Hello world"\n\t\t ^

10 Consume input example Example: If the following input from the user has been typed, John Smith "Hello world" The Scanner views it as a linear stream of data, like the following: 23\t3.14 John Smith\t"Hello world"\n\t\t \n The Scanner positions its 'cursor' at the start of the user input: 23\t3.14 John Smith\t"Hello world"\n\t\t \n ^ As we call the various next methods on the Scanner, the scanner moves forward: int x = console.nextInt(); // 23 23\t3.14 John Smith\t"Hello world"\n\t\t \n ^ double pi = console.nextDouble(); // \t3.14 John Smith\t"Hello world"\n\t\t \n ^ String word = console.next(); // "John" 23\t3.14 John Smith\t"Hello world"\n\t\t \n ^

11 Line-based input The Scanner's nextLine method consumes and returns an entire line of input as a String. The Scanner moves its cursor from its current position until it sees a \n new line character, and returns all text that was found. The new line character is consumed but not returned. Example: John Smith "Hello world" String line1 = console.nextLine(); 23\t3.14 John Smith\t"Hello world"\n\t\t \n ^ String line2 = console.nextLine(); 23\t3.14 John Smith\t"Hello world"\n\t\t \n ^

12 Mixing line-based with tokens It is not generally recommended to use nextLine in combination with the other next__ methods, because confusing results occur Joe "Hello world" int n = console.nextInt(); // 23 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^ double x = console.nextDouble(); // \t3.14\nJoe\t"Hello world"\n\t\t \n ^ // User intends to grab the John Smith "Hello world" line // but instead receives an empty line! String line = console.nextLine(); // "" 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^ // Calling nextLine again will get the line we wanted. String line2 = console.nextLine(); // "Joe\t\"Hello world\"" 23\t3.14\nJoe\t"Hello world"\n\t\t \n ^

13 Line-and-token example Here's another example of the confusing behavior: Scanner console = new Scanner(System.in); System.out.print("Enter your age: "); int age = console.nextInt(); System.out.print("Now enter your name: "); String name = console.nextLine(); System.out.println(name + " is " + age + " years old."); Log of execution (user input underlined): Enter your age: 12 Now enter your name: Marty Stepp is 12 years old. Why? User's overall input:12\nMarty Stepp After nextInt(): 12\nMarty Stepp ^ After nextLine():12\nMarty Stepp ^