i.
Hand back tests
i.
Consulting
hours
ii.
Questions?
i.
Documents,
html, .java, .class, pictures, etc…
i.
command line
arguments
ii.
scanner input
i.
new
Scanner(System.in);
ii.
new
Scanner(string);
i.
new
Scanner(file)
i.
hasNext(),
next(), nextLine(), etc
new Scanner(<txtFileName>); does not work
i.
You can do this
on one line with:
new Scanner(new File(<txtFileName>));
i.
An object of
PrintWriter represents text that will be written to a file
PrintWriter <varName> = new PrintWriter(<txtFileName>);
PrintWriter out = new PrintWriter(“output.txt”);
i.
If it doesn’t
exist, it is created in this directory.
ii.
If it already
exists, we empty it before writing
i.
print, println,
printf
ii.
If these look
familiar – they should.
1.
System.out is
actually an object of a class called PrintStream. PrintWriter is similar to this (an enhancement of it).
2.
Techincally:
System is a class name, out is a public static variable storing an object of
the PrintStream class
i.
.close()
ii.
If we don’t do
this, might not write everything to the file…
iii.
Due to how
write buffers work
i.
Java.io package
i.
We’ve seen
these before:
1.
Null Pointer
Exception
2.
IndexOutOfBounds
Exception
ii.
These are
run-time exceptions.
iii.
In general, we
can’t really ‘predict’ when they might happen.
If we make our code carefully, we can prevent them, though.
iv.
But, there are
some exceptions that are ‘out of our hands’, so to speak.
v.
For instance,
imagine if you’re trying to read from an input file, but the file does not
exist on your computer.
vi.
Your program
doesn’t check to see if it exists – it just assumes it does. But it’s possible that it doesn’t. Since this is a ‘predictable’ situation,
Java forces us to handle it elegantly.
vii.
In general:
There are some things that we can do in Java that will force Java us to do
something special to prevent a run-time error.
viii.
We must
‘handle’ the exception that can be thrown by a statement.
ix.
Unchecked vs
checked exceptions
i.
Throws and
try/catch
1.
We’ll do
try/catch next week
ii.
Throws today
<method declaration> throws FileNotFoundException
i.
We’re basically
telling Java that we know that this might not work, but it should go ahead and
do it anyway.
ii.
Note – this
doesn’t prevent errors at all. If the
file doesn’t exist, we’ll still error out
1.
It just informs
Java that we’re willing to accept that consequence
iii.
Note – you can
only declare this at the method level.
Doesn’t work at class level, line level, etc.