When we reach the line "z = x / y" the program crashes with the message:

Similar documents
Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

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

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

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

COMP Streams and File I/O. Yi Hong June 10, 2015

Streams and File I/O

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

HST 952. Computing for Biomedical Scientists Lecture 8

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

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

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

CS Programming I: File Input / Output

CS Programming I: File Input / Output

Input-Output and Exception Handling

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

Dining philosophers (cont)

First Java Program - Output to the Screen

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

Chapter 4: Loops and Files

ITI Introduction to Computing II

Lecture 19 Programming Exceptions CSE11 Fall 2013

ITI Introduction to Computing II

Oct Decision Structures cont d

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Simple Java Input/Output

Java Input/Output. 11 April 2013 OSU CSE 1

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Computer Science is...

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

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

What are Exceptions?

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

Chapter 4: Loops and Files

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Basic Computation. Chapter 2

IT101. File Input and Output

File Input and Output Review CSC 123 Fall 2018 Howard Rosenthal

DPCompSci.Java.1718.notebook April 29, 2018

Lecture 8 " INPUT " Instructor: Craig Duckett

Data Structures. 03 Streams & File I/O

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CSPP : Introduction to Object-Oriented Programming

5/24/2006. Last Time. Announcements. Today. Method Overloading. Method Overloading - Cont. Method Overloading - Cont. (Midterm Exam!

Principles of Computer Science I

Program 12 - Spring 2018

Exceptions Chapter 10. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Lecture 7. File Processing

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

1.00 Lecture 30. Sending information to a Java program

Unit 10: exception handling and file I/O

COMP-202 Unit 9: Exceptions

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

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Introduction Unit 4: Input, output and exceptions

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

File I/O and Exceptions

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Exceptions Handeling

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Exceptions Binary files Sequential/Random access Serializing objects

C17: File I/O and Exception Handling

PIC 20A Streams and I/O

CS 106 Introduction to Computer Science I

Lesson 3: Accepting User Input and Using Different Methods for Output

STUDENT LESSON A7 Simple I/O

St. Edmund Preparatory High School Brooklyn, NY

Programming with Java

BBM 102 Introduction to Programming II Spring Exceptions

COMP 202 Java in one week

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

Chapter 3 Java Exception

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

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

Introductory Programming Exceptions and I/O: sections

File I/O Array Basics For-each loop

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

13 th Windsor Regional Secondary School Computer Programming Competition

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

COMP 202. Java in one week

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

BBM 102 Introduction to Programming II Spring 2017

Basic Programming Elements

Streams and File I/O

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

Introduction to Computer Science II CS S-22 Exceptions

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

CSCI 1103: File I/O, Scanner, PrintWriter

Chapter 4 Introduction to Control Statements

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Computer Science 300 Sample Exam Today s Date 100 points (XX% of final grade) Instructor Name(s) (Family) Last Name: (Given) First Name:

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero.

Transcription:

CSCE A201 Introduction to Exceptions and File I/O An exception is an abnormal condition that occurs during the execution of a program. For example, divisions by zero, accessing an invalid array index, or trying to convert a letter to a number are instances of exceptions. Java gives you a way to anticipate the occurrence of an exception and write code to handle it. For example, consider the following code: int x = 50; int y = 0; int z = 0; z = x / y; System.out.println(z); When we reach the line "z = x / y" the program crashes with the message: Exception in thread "main" java.lang.arithmeticexception: / by zero At Excep.main It doesn t even continue on to the next line, but exits immediately. Now, let s see how to handle the exception ourselves. The exception itself is an object. For simple purposes, you can think of the exception as an error message that indicates what happened. The code or event that makes the exception happen is something that throws the exception. The code that handles what to do when the exception is thrown is code that will catch the exception. If we don t write code to catch the exception, then Java will catch the exception and halt the program (as well as display an error message). In order to catch an exception, Java requires that we place the code we think will cause the exception into a try block. The example below illustrates a simple example:

int x = 50; int y = 0; int z = 0; try // Place code we think might cause an // exception inside the try z = x / y; System.out.println("There was an exception!"); System.out.println("It was: " + e.getmessage()); System.out.println("The value in z = " + z); The try block contains the code we would like to execute but watch for an exception. This is followed by the catch block. Catch is followed by parentheses and takes a single parameter of type Exception. This is the most general class of Exception and is a parent to more specific types of exceptions. If an exception occurs in the try block, an object for that exception is created and is called "e". We can examine the exception or print it out to see what happened, and any other code inside the catch block is executed if the exception occurs. This program will generate an exception from the division and then begin execution in the catch block. It will output: There was an exception. It was: java.lang.arithmeticexception: / zero The value in z = 0 Note that the program continues execution after our catch block finishes. It doesn t crash and end as what happens when Java handles the exception!

Here is an example of a program that doesn t crash if we enter letters when we re expecting an integer: import java.io.*; public class Excep int i = 0; Scanner keyboard = new Scanner(System.in); boolean repeatloop = true; while (repeatloop == true) try i = keyboard.nextint(); // If we got here, there was no problem // parsing the integer repeatloop = false; System.out.println("There was a problem. "); System.out.println("Enter number again."); keyboard.nextline(); // Newline System.out.println("You input: " + i); This program doesn t actually print the exception, but if it occurs, we don t set the flag that prevents the outer while loop from exiting unless a number is entered, e.g. here is a sample execution, where bold is user input: hello There was a problem. Enter number again. four There was a problem. Enter number again. 4 You input: 4 There are derived classes from Exception that we can use, and it is usually a good idea to use the most specific class. For example, consider the following program that combines the two possible exceptions together:

import java.io.*; public class Excep String s = ""; int i = 0; int x = 10; Scanner keyboard = new Scanner(System.in); boolean repeatloop = true; while (repeatloop == true) try i = keyboard.nextint(); x = x / i; repeatloop = false; System.out.println("There was a problem. "); System.out.println("Enter number again."); keybard.newline(); System.out.println("X = " + x); By catching any exception, we can t tell the difference between a divide by 0 exception or a Number format exception. Consider the following execution: foo There was a problem. Enter number again. 0 There was a problem. Enter number again. 1 X = 10 We can distinguish the two types of exceptions by using more specific Exception classes that are derived from the generic class Exception. Here is an example where we catch three separate types:

import java.io.*; public class Excep String s = ""; int i = 0, x = 10; Scanner keyboard = new Scanner(System.in); boolean repeatloop = true; while (repeatloop == true) try i = keyboard.nextint(s); x = x / i; repeatloop = false; catch (ArithmeticException e) System.out.println("Don't enter zero."); catch (NumberFormatException e) System.out.println("Don t enter letters."); catch (IOException e) System.out.println("I/O exception " + e); System.out.println("X = " + x); Now we get different behavior for each type of exception when we run the program, e.g.: 0 Don t enter zero. Foo Don t enter letters. 10 X = 1 Refer to the Java help for a list of the Exception classes. Some common ones are: Exception - Any exception IOException - Problem with general I/O EOFException - Reading of end of line FileNotFound - File not found NumberFormatException - parse problem from string to number ArithmeticException - divide by 0, infinity, etc. There is more to exceptions, for example you can throw your own exceptions, but that covers the basics. See the text for more details.

File I/O I/O refers to input and output. So far we can do input from the keyboard and output to the screen. However, it is very useful to be able to take input from another file on disk. Here we will cover only the basics for inputting data from a text file (a file containing only ASCII text data). The textbook has details about reading binary files (a file containing direct bytes of information), which are generally more efficient than text files. I/O in Java is based on the concept of a stream. A stream is a flow of data, where the data could be characters, bytes, or numbers. An input stream is when data flows into your program, which we have been using via System.in. An output stream is when data flows out of your program, which we have been using via System.out. A file will have two names : one will be the name of the file on the disk, and the other will be the name of the stream that is connected to the file. Reading data from a text file The process of reading data from a text file is similar to the above for setting up the file. Once the file is open, reading from the file is just like reading from the keyboard. The only difference is that the input data will be coming from the file instead of being typed by the user. Let s say that we have a file on the root of our c:\ drive named indata.txt. The file contains the following data: c:\indata.txt hello world 300 400 Here is a program that can read in this data. Reading in data is very similar to reading from System.in: import java.io.fileinputstream; import java.util.scanner; // Need this to read a file public class FileInput int val1 = 0, val2 = 0; String s; Scanner inputstream = null; try inputstream = new Scanner(new FileInputStream("c:\\indata.txt")); // If we got here, the file was opened

s = inputstream.nextline(); val1 = inputstream.nextint(); val2 = inputstream.nextint(); System.out.println("Line = " + s + " val1 = " + val1 + " val2 = " + val2); inputstream.close(); // If any exception occurs, exit System.out.println("Error " + e); System.exit(0); We need the two slashes in c:\\indata.txt because \ is the escape character. Two slashes means one slash, so this is equivalent to the string c:\indata.txt. If you don t put a full pathname (i.e. only put the name of the file) then Java will look for the file in the same folder as your Java class files. When we open the file, it should be inside a try/catch block. When reading from a file, an exception might occur (such as the file doesn t exist or is corrupted). First, here is what we get if the file indata.txt is not located on the c:\ drive, we stop executing code in the try block and hit the catch: Error java.io.filenotfoundexception: c:\indata.txt (the system cannot find the file specified) Here is what we get if the file does exist with the data: Output: s = hello world val1 = 300 val2 = 400 The book also has a description of the StringTokenizer class that makes it easy to extract values separated by some delimiter. Here is another example that reads in words from a dictionary file to solve this word puzzle: "Name a common word, besides tremendous, stupendous and horrendous, that ends in dous." We can solve this problem by loading up a file of words and checking each one to see if: 1) It is more than 4 letters long 2) The word contains dous at the end

Our strategy will be to read each line of the file, see if it contains the text "dous" and if it does then print it out. This will result in some false positives since "dous" might be found mid-word, but there should not be many of these cases. We can just visually inspect the list of words for the fourth word that ends in "dous. " We can keep reading a line until the hasnextline( ) method returns false. This occurs when we've read every line in the file. Assume we have a file of English words located at C:\WORDS.TXT, code follows: import java.io.fileinputstream; import java.util.scanner; public class WordFinder String s; Scanner inputstream = null; try inputstream = new Scanner(new FileInputStream("c:\\words.txt")); while (inputstream.hasnextline()) s = inputstream.nextline(); s = s.tolowercase(); if (s.indexof("dous")>0) // Candidate word, print it out System.out.println(s); inputstream.close(); // If any exception occurs, exit System.out.println("Error " + e); System.exit(0); Exercise: Modify the trivia game from the Array lecture so the trivia questions are read in from a file instead of hard-coded in the program.

Saving data to a text file First let s see how to save data to a text file. To do so, we will use a class called PrintWriter. Here is an example program that creates a file named output.txt and saves the numbers 1 through 10 in it: import java.io.printwriter; import java.io.fileoutputstream; public class TextFile int i; PrintWriter outdata = null; // Initialize stream to empty // First we must open the file. Opening must be // inside a try/catch block try outdata = new PrintWriter( new FileOutputStream("output.txt")); System.out.println(e); // Print error System.exit(0); // Exit if an error // If we get to this point, the file was // successfully opened for (i=0; i<10; i++) // Println outputs to file instead of screen outdata.println("number " + i); outdata.close(); // Close file when done This program declares a variable of type PrintWriter called outdata. This will be a variable connected to our file for output. Initially it is set to null, which is empty. To create the file, we create a new PrintWriter with a new FileOutputStream object. This particular invocation will destroy any file that might currently exist with the same name in the same directory as the new file. The parameter to the FileOutputStream object is a string that is the name of the file we want to create. If we give it just a file name, as in output.txt then when this program is run it will create a new file called output.txt in the current directory. If you re using NetBeans then it will create this file in your project directory.

Note that we could also give a complete path if we wanted to make the file somewhere else. The following would have created the file on the root of the C: drive: outdata = new PrintWriter( new FileOutputStream("c:\\output.txt")); To save data to the text file, we use the stream variable followed by the println method. Println behaves just like we ve been doing with System.out.println. In this example, we are just outputting ten lines with numbers in them. When we re done writing to the file, make sure to close the file with the close() method. This program creates a file named output.txt in the default directory with the contents: Number 0 Number 1 Number 2 Number 3 Number 4 Number 5 Number 6 Number 7 Number 8 Number 9