CIS 110: Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
CS102--Object Oriented Programming
Advertisements

More Java Syntax. Scanner class Revisited The Scanner class is a class in java.util, which allows the user to read values of various types. There are.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
CIS 1068 Program Design and Abstraction
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 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 6 Lecture 6-1: File Input with Scanner reading: , 5.3 self-check: Ch. 6 #1-6.
1 CSE 142 Lecture Notes File input using Scanner Suggested reading: , Suggested self-checks: Section 6.7 # 1-11, These lecture.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Copyright 2010 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 2006 by Pearson Education 1 Building Java Programs Chapter 6: File Processing.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 BUILDING JAVA PROGRAMS CHAPTER 6 FILE PROCESSING.
1 BUILDING JAVA PROGRAMS CHAPTER 6 DETAILS OF TOKEN-BASED PROCESSING.
BUILDING JAVA PROGRAMS CHAPTER 6 File Processing.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Building Java Programs File Processing. 2 Input/output (I/O) import java.io.*; Create a File object to get info about a file on your drive. –(This doesn't.
Building Java Programs Chapter 6 Lecture 6-2: Line-Based File Input reading:
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1.
Copyright 2010 by Pearson Education Building Java Programs Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Lab 04-2 Objectives:  Understand what a file is.  Learn how to use the File class  Learn how to use the Scanner class to read from files  Learn about.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Building Java Programs Chapter 6 File Processing Copyright (c) Pearson All rights reserved.
Building Java Programs
File - CIS 1068 Program Design and Abstraction
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Building Java Programs
Building Java Programs
File Input and Output TOPICS File Input Exception Handling File Output.
Building Java Programs
Creating and Modifying Text part 2
File Input and Output TOPICS File Input Exception Handling File Output.
CSS161: Fundamentals of Computing
Advanced Java Programming
CSS 161: Fundamentals of Computing
Building Java Programs
Introduction to Classes and Methods
Building Java Programs Chapter 6
Building Java Programs Chapter 6
Building Java Programs
CS 106A, Lecture 10 File Reading
Input/output (I/O) import java.io.*;
Building Java Programs
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Building Java Programs
Building Java Programs
Building Java Programs
Input/output (I/O) import java.io.*;
CMSC 202 Exceptions 2nd Lecture.
Building Java Programs
Building Java Programs
Building Java Programs
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Topic 19 file input, line based
CMSC 202 Exceptions 2nd Lecture.
Building Java Programs
Chapter 6 Lecture 6-1: File Input with Scanner reading: 6.1 – 6.2, 5.4
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Building Java Programs
Presentation transcript:

CIS 110: Introduction to Computer Programming Lecture 15 Our Scanner eats files (§ 6.1-6.2) 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Outline Programming assertion recap The Scanner object and files Token-based file processing 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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. 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

Programming assertions revisited 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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.) 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

The Scanner object and files 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Scanners revisited A Scanner is a faucet over some pipe of data. Scanner System.in 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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) 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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

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 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

Consuming the rest of a line We can consume the rest of a line with nextLine(). Scanner System.in \n \n 42 world! Hello 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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")); 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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. 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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")); } Which do we use!? try { Scanner file = new Scanner( new File("data.txt")); } catch (FileNotFoundException ex) { ex.printStackTrace(); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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")); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

Token-based processing 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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")); 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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); 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

A file is just a long-ass string data.txt We can think of a file as a sequence of characters by replacing newlines with '\n' 3.4 7.1 4.3 5.9 1.1 2.5 3.6 -1.2 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(); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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(); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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 On each call to nextDouble, we consume the next double and advance the input just past the token. Input cursor data.txt while(file.hasNextDouble()) { double d = file.nextDouble(); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

Jumping that whitespace 3.4 7.1 4.3\n 5.9 1.1 2.5\n\n\n3.6 -1.2\n Calls to nextX() skip whitespace to the next token. Input cursor data.txt while(file.hasNextDouble()) { double d = file.nextDouble(); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

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(); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania 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(); } 8/19/2019 CIS 110 (11fa) - University of Pennsylvania

CIS 110 (11fa) - University of Pennsylvania Mixing up types We can mix different nextX functions as necessary. Jerry 21 13.7 true file.next(); file.nextInt(); file.nextDouble(); But we get NoSuchElementException if we're wrong! file.nextBooelean(); 8/19/2019 CIS 110 (11fa) - University of Pennsylvania