Lecture 35, CS 302-7 and 8, April 18

  1. Various things
    1. Program 3
  2. Review:
    1. Reading Files

                                                               i.      java.io.File;

                                                             ii.      java.util.Scanner;

1.      new Scanner(File f);

    1. Writing Files

                                                               i.      java.io.PrintWriter;

    1. Exceptions

                                                               i.      throws

                                                             ii.      Java.io.FileNotFoundException;

    1. Command line arguments
  1. Various things
    1. File Reading – backslashes

                                                               i.      Say you’re trying to read a file from some location on your computer other than the current directory.

                                                             ii.      You’ll need to specify a full file name.

                                                            iii.      On Windows machines, these include the \ character.

1.      Note – on Linux/Mac machines, you don’t need to worry about this…

                                                           iv.      Why might this be a problem?

1.      Escape characters – eg \n for newline.

2.      \\ is similar, except it means “Single \”

                                                             v.      So, “c:\\directory\\file.txt”

                                                           vi.      You probably won’t have to do this very often, though

                                                          vii.      Note – this only applies in your code, not in what you supply to the program at run-time (eg user input)

                                                        viii.      Note – what if you’re not sure your program will be run on Windows or Mac

1.      File.separator

2.      This is a static String in the File class

3.      Represents the correct separator character (\\ or /) for your system

    1. So, you can write the same program for either platform
  1. HTML example
    1. You can use a Scanner to read a webpage
    2. URL class

                                                               i.      In java.net package

                                                             ii.      Object represents the address of a webpage

                                                            iii.      Scanner based on url.openStream() [which returns an inputStream object]

    1. This throws an IOException
    2. HTMLReader.java

                                                               i.      Apply to cs302 website

  1. scanner.next() details
    1. scanner.useDelimiter(String delim)

                                                               i.      Default delimiter - whitespace

                                                             ii.      The book’s discussion about options for the parameter here is not very satisfying

1.      In general it accepts something called regular expressions

a.       These are like patterns

b.      We won’t worry about the details in this class

2.      But, you can also provide a specific string that you want Java to use as a delimiter for the items in the Scanner

                                                            iii.      Simple example…

1.      Specify String for delimiter, such as “,” or “:”

a.       With delimiter “,”, the following: “Hello,goodbye” is parsed into “Hello” and “goodbye”

2.      This overrides the default – whitespace will no longer separate parts of the input if you specify a delimiter

3.      Can be more than one character long, such as “…“ or “, “

a.       If you use more than one character, it must match the string exactly

b.      So, with the delimiter “…”, the following will be returned as one String: “a, b. c d..e”

4.      useDelimiter(“”) – read one character at a time

  1. Exam 2
    1. To get percentage – Total/72
    2. Class average – 68% ~49/72