CIS 110: Introduction to Computer Programming

Similar documents
Outline. CIS 110: Introduction to Computer Programming. Exam announcements. Programming assertions revisited. An extended example: mystery

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Building Java Programs

Reading Input from Text File

A token is a sequence of characters not including any whitespace.

Building Java Programs

Excep&ons and file I/O

AP Computer Science. File Input with Scanner. Copyright 2010 by Pearson Education

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

EXCEPTIONS. Fundamentals of Computer Science I

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

File I/O Array Basics For-each loop

CS 112 Introduction to Programming

File Processing. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. A Class for Representing a File

Computer Science is...

Input-Output and Exception Handling

Computational Expression

Building Java Programs

CS244 Advanced Programming Applications

CS 112 Introduction to Programming

Lecture 19 Programming Exceptions CSE11 Fall 2013

1. An operation in which an overall value is computed incrementally, often using a loop.

Programming II (CS300)

GPolygon GCompound HashMap

Building Java Programs Chapter 6

Building Java Programs

Programming II (CS300)

CS159. Nathan Sprague

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

AP Computer Science Unit 1. Programs

Building Java Programs

BBM 102 Introduction to Programming II Spring Exceptions

Reading Text Files. 1 Reading from text files: Scanner

File I/O Introduction to File I/O Text Files The File Class Binary Files 614

CS 112 Introduction to Programming

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

AP Computer Science Unit 1. Writing Programs Using BlueJ

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

Building Java Programs

Program 12 - Spring 2018

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Comp 249 Programming Methodology Chapter 9 Exception Handling

16-Dec-10. Consider the following method:

An exception is simply an error. Instead of saying an error occurred, we say that an.

Building Java Programs

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

Midterms Save the Dates!

Unit 10: exception handling and file I/O

Outline. CIS 110: Introduction to Computer Programming. Any questions? My life story. A horrible incident. The awful truth

CIS 110: Introduction to Computer Programming

Programming with Java

CS159. Nathan Sprague

Programming Languages and Techniques (CIS120)

CS 112 Introduction to Programming

CS 112 Introduction to Programming

Introduction to Java Unit 1. Using BlueJ to Write Programs

ITI Introduction to Computing II

ITI Introduction to Computing II

Midterm Exam 2 Thursday, November 15th, points (15% of final grade) Instructors: Jim Williams and Marc Renault

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CS 302: Introduction to Programming in Java. Lectures 17&18. It s insanely hot. People desperately need some snowflakes

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories

CS 3 Introduction to Software Engineering. 3: Exceptions

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1

AP Computer Science Unit 1. Writing Programs Using BlueJ

COMP102: 181. Menu. More while loops. Admin: Test. Peter Andreae

Advanced Java Concept Unit 1. Mostly Review

Full file at

11/1/2011. Chapter Goals

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

Building Java Programs

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7

Object Oriented Programming Exception Handling

Exceptions in Java

CS 112 Programming 2. Lecture 08. Exception Handling & Text I/O (1) Chapter 12 Exception Handling and Text IO

CSCI 1103: File I/O, Scanner, PrintWriter

More on Exception Handling

Programming Languages and Techniques (CIS120)

More on Exception Handling

CSCI 1103: File I/O, Scanner, PrintWriter

Text User Interfaces. Keyboard IO plus

Building Java Programs

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Files. Reading data from files. File class. Compiler error with files. Checked exceptions. Exceptions. Readings:

Building Java Programs

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

CIS 110: Introduction to Computer Programming

CSPP : Introduction to Object-Oriented Programming

Introduction to Computer Science II CS S-22 Exceptions

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

Chapter 12 Exception Handling

Chapter 12 Exception Handling and Text IO. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Transcription:

CIS 110: Introduction to Computer Programming Lecture 15 Our Scanner eats files ( 6.1-6.2) 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 1

Outline Programming assertion recap The Scanner object and files Token-based file processing 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 2

Exam announcements Attempting to reschedule midterm #2 to 11/21 (Monday of Thanksgiving break) Let me know asap if you will be out of town. Final time has been confirmed for 12/19, 6-8 PM Let me know asap if you need to reschedule. 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 3

Programming assertions revisited 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 4

An extended example: mystery public static int mystery(int n) { int x = 0; // Point A if (n < 0) { return -1; while (n!= 0) { // Point B int d = n % 10; if (d % 2 == 1) { x += d; // Point C n /= 10; // Point D return x; For each point, are the following always/sometimes/never true? 1) n < 0 2) x >= 0 3) d < 10 4) x < n (See AssertionProblem.java.) 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 5

The Scanner object and files 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 6

Scanners revisited A Scanner is a faucet over some pipe of data. Scanner System.in 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 7

Empty pipes If the pipe is empty, the scanner first gets a line of input from the user, e.g., one call to next(). Scanner System.in Hello world! 42\n 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 8

Consuming input from the pipe The call to next() then consumes the first token of input. Scanner System.in world! 42\n Hello (Token = chunk of text separated by whitespace) 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 9

Consuming tokens When we consume a token, there's no way to "go back", only forward! Scanner System.in 42\n world! Hello 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 10

Consuming tokens as different types We can consume a token and translate it to a particular type, e.g., nextint(). Scanner System.in \n 42 world! Hello 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 11

Consuming the rest of a line We can consume the rest of a line with nextline(). Scanner System.in \n \n 42 world! Hello 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 12

Plugging in different data sources A Scanner can accept many kinds of data sources such as Files instead! Scanner File Scanner file = new Scanner(new File("data.txt")); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 13

The File object A File object represents a file or directory on disk. Exists in the java.io package. File file = new File("data.txt"); System.out.println("canRead? " + file.canread()); System.out.println("exists? " + file.exists()); // Renames the file to the given file's name. file.renameto(new File("foo.txt")); // Deletes the file from disk if it exists. file.delete(); File 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 14

An exceptional problem public static void main(string[] args) { Scanner file = new Scanner(new File("data.txt")); The following code fails to compile. Why? "unreported exception java.io.filenotfoundexception; must be caught or declared to be thrown" Example of a checked exception in Java. 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 15

Checked and unchecked exceptions Java distinguishes between two sorts of exceptions. Unchecked exceptions represent program bugs Checked exceptions represent badness outside of the program's control. Unchecked exceptions IndexOutOfBoundsException IllegalArgumentException StackOverflowError Checked exceptions FileNotFoundException 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 16

Dealing with checked exceptions Two solutions: Annotate the enclosing method with a throws clause. Use a try-catch block. public static void main(string[] args) throws FileNotFoundException { Scanner file = new Scanner(new File("data.txt")); try { Scanner file = new Scanner( new File("data.txt")); catch (FileNotFoundException ex) { ex.printstacktrace(); Which do we use!? 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 17

Checked exceptions: a holy war Big debate if checked exceptions are "worth it". General advice: use try-catch when you can do something meaningful with the exception. Give a good error message, re-throw, etc. For this class: we'll use throws clauses. public static void main(string[] args) throws FileNotFoundException { Scanner file = new Scanner(new File("data.txt")); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 18

Token-based processing 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 19

Abstraction at its finest The methods of the Scanner we've learned so far apply when we use a File instead! Scanner File Scanner file = new Scanner(new File("data.txt")); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 20

File processing example: FileSum import java.util.*; // Necessary since FileNotFoundException is also in java.io. import java.io.*; public class FileSum { public static void main(string[] args) throws FileNotFoundException { Scanner file = new Scanner(new File("data.txt")); double sum = 0.0; while(file.hasnextdouble()) { double d = file.nextdouble(); System.out.println("Adding up " + d + "..."); sum += d; System.out.println("Total = " + sum); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 21

A file is just a long-ass string data.txt 3.4 7.1 4.3 5.9 1.1 2.5 3.6-1.2 We can think of a file as a sequence of characters by replacing newlines with '\n' 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6-1.2\n while(file.hasnextdouble()) { double d = file.nextdouble(); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 22

Input cursor 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6-1.2\n Input cursor The input cursor is our current position in the file, initially at the beginning. data.txt while(file.hasnextdouble()) { double d = file.nextdouble(); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 23

Input cursor and input consumption 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6-1.2\n Input cursor On each call to nextdouble, we consume the next double and advance the input just past the token. data.txt while(file.hasnextdouble()) { double d = file.nextdouble(); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 24

Jumping that whitespace 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6-1.2\n Input cursor Calls to nextx() skip whitespace to the next token. data.txt while(file.hasnextdouble()) { double d = file.nextdouble(); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 25

Newlines are whitespace 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6-1.2\n Input cursor 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6-1.2\n Input cursor data.txt while(file.hasnextdouble()) { double d = file.nextdouble(); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 26

hasnextx looks ahead 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6-1.2\n hasnextdouble() now returns false! Input cursor data.txt while(file.hasnextdouble()) { double d = file.nextdouble(); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 27

Mixing up types We can mix different nextx functions as necessary. Jerry 21 13.7 true file.next(); But we get NoSuchElementException if we're wrong! file.nextint(); file.nextdouble(); file.nextbooelean(); 10/31/2011 CIS 110 (11fa) - University of Pennsylvania 28