


nextInt/nextDoubleHave a look at Homework 4C.
We need to read two files with the same format.
That's an obvious case for a method that reads a file and returns the data in the file (boy and girl names).
What should it return?
ArrayList<String>[] of length 2, holding a list of boy names and a list of girl namesString[][] of String[] arrays of length 2, holding an array of boy names and an array of girl namesArrayList<ArrayList<String>> of length 2, holding a list of boy names and a list of girl namesArrayList<Person>, where Person is a class whose instances have a name and a genderpublic class BankAccount
{
public void withdraw(double amount)
{
if (amount > balance)
{
throw new IllegalArgumentException("Amount exceeds balance");;
}
balance = balance - amount;
}
. . .
}
public static Comparable smallest(Comparable[] values)
{
if (values.length == 0) throw new XXXException(); // What should we throw?
Comparable smallestSoFar = values[0];
for (int i = 1; i < values.length; i++)
if (values[i].compareTo(smallestSoFar) < 0)
smallestSoFar = values[i];
return smallestSoFar;
}
ArrayIndexOutOfBoundsExceptionIllegalArgumentExceptionIllegalStateExceptionNoSuchElementExceptionnew Scanner(file) can throw FileNotFoundExceptionthrows to alert the caller
public static void process(String filename) throws FileNotFoundException public static void main(String[] args) throws FileNotFoundException
When you call a[i] on an array, your code might throw an ArrayIndexOutOfBoundsException. Why don't you have to tag methods with throws ArrayIndexOutOfBoundsException?
ArrayIndexOutOfBoundsException is a checked exceptionArrayIndexOutOfBoundsException is checked at compile-timei is validNullPointerException (if a is null)try { String filename = . . .; FileReader reader = new FileReader(filename); Scanner in = new Scanner(reader); String input = in.next(); int value = Integer.parseInt(input); . . . } catch (IOException exception) { exception.printStackTrace(); } catch (NumberFormatException exception) { System.out.println("Input was not a number"); }
throws, catch 
try
{
process(filename)
}
catch (FileNotFoundException ex) { println("File not found"); }
catch (IOException ex) { println("I/O error"); }
catch (RuntimeException ex) { println("Something went wrong"); }
Suppose process throws a EOFException. What gets printed?
reader = new FileReader(filename); Scanner in = new Scanner(reader); readData(in); reader.close(); // May never get here
no matter what
Scanner in = new Scanner(file);
try
{
readData(in);
}
finally {
in.close();
}
public void read(String filename)
{
Scanner in = null;
try
{
in = new Scanner(new File(filename));
process(in);
}
catch (IOException ex) { println("I/O Error"); }
finally { in.close(); }
}
Which of the following is true if the file does not exist?
FileNotFoundException is thrown to the caller of readNullPointerException is thrown to the caller of readread method returns without printing anything and without an exceptiontry (Scanner in = new Scanner(file))
{
readData(in);
} // in.close() called automatically—Scanner is AutoCloseable
in is the inside of the {...}try (Scanner in = new Scanner(...); PrintWriter out = new PrintWriter(...))
AikenReader throws exception when there is a problemAikenReaderDemo catches it, displays, and terminates cleanly