i. Due tonight
i. Reading word-by-word
1. Custom delimiters
ii. Reading line-by-line
1. Three ways to parse words out a line of text
in a String s:
a. String and Character methods, such as
s.substring(index1, index2) and s.indexOf(searchString)
b. new Scanner(s)
c. s.split(delim)
iii. Reading numbers
1. scanner.nextInt() doesn’t handle newline
characters
i. Why now?
1. Often used for passing filenames into
programs
2. Can result in/throw exceptions
i. Recognizing that an exception has
occurred. For instance, unable to open
the filename specified.
i. Doing something productive with that
exception
ii. What exactly this means will depend on the
context of the exception
1. Different exceptions/situations require
different handling.
if(/*Something bad happens*/) {
throw new <Type of
Exception>();
}
if(/*Something bad happens*/) {
throw new <Type of
Exception>(“Abort! Something bad happened!”);
}
i. try – we’ll try to execute some code. It might throw an exception before it is
completed.
ii. catch – we’ll catch a specific type of
exception that might occur in the code, and handle it accordingly.
try {
//Code that might cause
exception
}
catch(<Type of exception> <object name>)
{
//Do something, such as:
//<objectName>.printStackTrace();
}
i. These are issues related to our computer or
something else, such as running out of memory.
We won’t worry about these.
i. These are exceptions that are
‘preventable’. They are the result of
buggy code.
ii. For instance: ArrayIndexOutOfBoundsException,
NullPointerException
iii. Java does not require us to handle these
i. These are exceptions that we can’t predict
ii. For instance: FileNotFoundException,
IOException
iii. Java requires us to handle these
i. If we say that the method throws that
exception, it will be passed up the call stack to the method that called this
method.
ii. If we handle the exception with a try/catch
block, we do not keep searching up the call stack – the JVM is satisfied.
iii. If there is an exception that can be thrown
by the code in the try block, but it is not handled in a catch block, we still
must acknowledge that the method throws that exception – we will then search up
the call stack hierarchy.