Lecture 35, CS 302-6, November 23

  1. Various things
    1. Program 3

                                                               i.     New due date - Monday, November 28, 10pm

                                                             ii.     No consultants scheduled over break

1.    I might be reachable by e-mail, but don’t rely on it

                                                            iii.     You should redownload the jar file (you might have to make some slight changes to your code)

  1. Review:
    1. Reading Files

                                                               i.     java.io.File;

                                                             ii.     java.util.Scanner;

1.    new Scanner(File f);

2.    new Scanner(URL u.openStream());

                                                            iii.     java.net.URL;

    1. Writing Files

                                                               i.     java.io.PrintWriter;

    1. Exceptions

                                                               i.     throws

                                                             ii.     Java.io.FileNotFoundException;

                                                            iii.     java.io.IOException;

  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”

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

  1. Reading/Parsing lines of text
    1. Option one – parse the String directly using String, Character, and number methods

                                                               i.     boolean Character.isDigit()

1.    Is this character a number between 0 and 9?

                                                             ii.     boolean Character.isWhiteSpace()

1.    Is this a whitespace character?

                                                            iii.     String string.trim()

1.    Removes whitespace from beginning and end of string

a.    This includes newlines

                                                           iv.     int Integer.parseInt()

1.    What if it’s not an int?  Or even if it contains whitespace…

    1. Option two – Scanner

                                                               i.     new Scanner(String s);

                                                             ii.     Use hasNext(), next(), etc

    1. Option three – split String

                                                               i.     String[] string.split(String delim);

1.    Returns an array containing the individual pieces of the String according to the delimiter we pass in

2.    like with useDelimiter, we specify what to split around

a.    Another example of a regular expression, where we can also use something simpler

3.    We’ll usually use something like “ “ or “,”

  1. When we should use scanner.nextLine() after scanner.nextInt()
    1. nextInt() doesn’t read whitespace, newlines, etc
    2. So, if we store a bunch of integers in a file, on separate lines, we’ll need to call nextLine() after we get each one.
  2. Command line arguments – why does the book introduce them now?
    1. Because they’re often used for inputting file names
    2. Why are these useful?  Why not just take info as input(Scanner)/GUI input?

                                                               i.     Automating – batch file, shell scripts, etc

1.    Basically, when you have to do something a bunch of times, with different arguments – easier to automate than do it by hand

  1. Homework – For Monday – finish reading Ch. 8, finish program 3