1 of 20

Files, Exceptions, and Abstractions: Lecture #7

Douglas Blank, Bryn Mawr College, CS206, Spring 2013

2 of 20

Similarities between Array and HashMap

Very similar in the abstract: they both have constant-time in setting and getting values

  • Both behave O(1) setting value
    • Array[position] = value
    • HashMap.put(key, value)
  • Both behave O(1) getting value
    • Array[position]
    • HashMap.get(key)

3 of 20

Differences between Array and HashMap

  • Memory
    • Arrays allocate fixed, full, contiguous memory for all possible array locations
    • HashMaps allocate memory as needed, and can store arbitrary amount of items
    • Arrays are typically dense: all positions have values
    • HashMaps are typically sparse: keep collisions to a minimum
  • Keys
    • Arrays uses an integer as "key" (it is the location in the array)
    • HashMaps can use any object as key, as it uses the object's .hashCode()

4 of 20

When something bad happens

  • Beyond Syntax errors, beyond Logical errors, things can go wrong in a computer program
  • We call these "exceptions"
  • There are places in Java that well-defined exceptions can occur
    • Accessing the Internet
    • Reading in a file from your harddrive
    • etc.
  • Exceptions in Java must either be "caught" or "thrown"

5 of 20

Exceptions: rule of thumb

  • Throw them and let some other code handle the issue
  • At the "highest place" you can, catch the exception and give an error message

Otherwise, you might be calling lower level code, and it just "eats" the problem without any indication that something is wrong.

6 of 20

Catch and Throw

void someMethod() {

whereSomethingBadCanHappen();

}

7 of 20

Catch and Throw

void someMethod() throws Exception {

whereSomethingBadCanHappen();

}

8 of 20

Catch and Throw

void someMethod() {

try {

whereSomethingBadCanHappen();

} catch (Exception e) {

// recover

}

}

9 of 20

Input from a File

import java.io.*;

public class TestIO {

public static int countLines(File f) {

}�public static void main(String[ ] args) {

}

}

10 of 20

Input from a File

Would like to read a line from a text file

How to find the right Java class?

  • Experience
  • Textbook
  • Internet Search

11 of 20

Input from a File: BufferedReader

  1. To read a line at a time, we need a BufferedReader
  2. BufferedReader needs an InputStreamReader
  3. InputStreamReader needs an FileInputStream
  4. FileInputStream needs a File
  5. File needs a String

12 of 20

Input from a File

BufferedReader br = new BufferedReader(� new InputStreamReader(� new FileInputStream(� new File("test.txt"))));

br.readLine() // returns null when done

br.close()

13 of 20

Input from a File: throw

public static int countLines(File f) throws IOException {� BufferedReader br = new BufferedReader(� new InputStreamReader(� new FileInputStream(f)));

int count = 0;� String line = br.readLine();� while (line != null) {� count++;

line = br.readLine();� }� br.close();� return count;� } �

14 of 20

Input from a File: throw

import java.io.*;

public class TestIO {

...

public static void main(String[ ] args) throws IOException {

System.out.println(countLines(new File("Fileio.java")));

}

}

15 of 20

Input from a File: catch

public static int countLines(File f) {

int count = 0;� try {� BufferedReader br = new BufferedReader(� new InputStreamReader(� new FileInputStream(f)));

String line = br.readLine();� while (line != null) {� count++;

line = br.readLine();� }� br.close();

} catch (IOException e) {

}� return count;� } �

16 of 20

Input from a File: catch

import java.io.*;

public class TestIO {

...

public static void main(String[ ] args) {

System.out.println(countLines(new File("Fileio.java")));

}

}

17 of 20

Interactive Game

String line = br.readLine();

while (line != null) {

...

line = br.readLine();

}

18 of 20

Game File

state: Field

descr: You are in a beautiful field.

command: north

to: Dungeon

state: Dungeon

descr: You are in the danky dungeon. � You smell onions.

command: south

to: Field

19 of 20

Interactive Game

String line = br.readLine();

while (line != null) {

if (line.StartsWith("state:")) {

} else if (line.StartsWith("descr:")) {

}

line = br.readLine();

}

20 of 20

Interactive Game

Let's write some code...