You may also find it helpful to consult related example programs on arrays, file processing, and recursion, including the following files:
In my CityDatabase
program, I get the following
error message:
Unhandled exception type FileNotFoundException
Can you remind me of how to address this?
You need to add “throws FileNotFoundException
” to the header of the
method containing the line that is generating this error message – as
well as to the header of other methods that call that method, and any
methods that call those methods.
In problem 6, option 1, I’m not finding results for any year. What am I doing wrong?
First, make sure that you are using the using the Scanner
object’s nextLine()
method to read in the year from the
user, rather than the nextInt()
method (see guideline 3).
This will give you the year as a string, which will make it
easier to compare with the year field that you obtain from the
split()
method, because split()
returns an array of String
s.
Second, make sure that you are using the equals()
method and
not the ==
operator to compare the year you obtained from the
user with the year field from a given record.
In problem 6, option 2, I’m unclear about what I should be storing in the results array.
We recommend storing the entire record – i.e., the entire line
that was returned by the file Scanner
‘s nextLine()
method.
That way, you will have all of the fields of the record, and you
can later re-split the record when printing the results.
In problem 6, option 2, after we create an array for the results, do we need to initialize it?
No. The array will be filled with null
s by default.
My code for problem 6, option 2 seems to work for most cities, but not all all of them. For example, it finds no results for Kansas City, MO. Do you have any suggestions?
Here’s one thing to check: Make sure that you’re using the Scanner
object’s nextLine()
method to read in the city name from the user.
Doing so will allow you to handle multi-word city names.
In one of my methods for problem 8, I’m getting a
StringIndexOutOfBoundsException
. What am I doing wrong?
Don’t forget that the characters in a string have index values that go from 0 to length - 1. The exception means that your code is using an index from outside that range.
There are several reasons this might be happening:
substring
method with invalid index values.You are failing to correctly detect the case in which the string is empty, because you are doing something like this:
if (str == null || str == "") { ...
Because str
is a string, you need to use the equals
method
instead of ==, as shown below:
if (str == null || str.equals("")) { ...
You are failing to return after dealing with a case in which the
string is empty. For example, consider the following incorrect
version of the printVertical
method from lecture:
public static void printVertical(String str) { if (str == null || str.equals("")) { System.out.println(); // INCORRECT: should return here, but doesn't } System.out.println(str.charAt(0)); printVertical(str.substring(1)); }
Note that the method fails to return after handling the case
in which str
is either null
or empty, and thus the method
still attempts to extract the first character, which causes an
exception.
You are attempting to extract a character before checking for the case in which the string is null or empty. You should be sure to handle those cases first – at the very start of the method.
In one of my methods for problem 8, I’m getting a
NullPointerException
, even though I am checking for the case in
which str
equals null
. What am I doing wrong?
Make sure that the first thing that your method checks for is a
null
value. You need to check for and handle it by doing
something like this:
if (str == null) { return 0; }
although the actual return value will depend on which method you’re writing.
For some of the methods, you may be able to combine the above test
with another test, like we do in the numOccur
method that we
gave you.
In one of my methods for problem 8, I’m using the substring
method to create a substring that has everything but the first
character from the original string. I’m using the expression
str.substring(1, str.length() - 1)
, but I seem to be losing
characters at the end of the string as well.
Don’t forget that the second parameter to the substring
method
is non-inclusive. So if you want the last character to be included
in the substring, your should use the expression str.substring(1,
str.length())
– without the “- 1”. Alternatively, you could use
the version of substring
that only takes a start index:
code.substring(1)
.
In one of my methods for problem 8, it seems like I would need two or more base cases. Is it possible to have a recursive method with multiple base cases?
Yes! See our palindrome example for an illustration of this.
For part 2 in problem 8, it says that we should not print
anything if the parameter is either null
or the empty string. Is
there a way to get the method to just return?
Yes. Don’t forget that a void
method can still have a return
statement:
if (condition) { return; }
I’m having trouble getting one of my methods from problem 8 to work correctly. Do you have any suggestions?
Try tracing through some concrete examples of cases in which the your method is not returning the correct value. You might want to try adding some temporary printlns (like we do in the NumOccurTrace example program) to see if that helps you to diagnose the problem. In particular, you could print what the method will return before it actually returns it. That will allow you to see when the wrong value is being returned.
It can also help to write out the sequence of recursive calls that would be made for one of more concrete cases. For example:
numOccur("banana", 'a') numOccur("anana", 'a') numOccur("nana", 'a') numOccur("ana", 'a') numOccur("na", 'a') numOccur("a", 'a') numOccur("", 'a')
Then, once you have the sequence of method calls, you should think
about what each of these separate method calls should return,
treating them as if they were independent of each other. For
example, what should numOccur("ana", 'a')
return – i.e., how
many times does 'a'
appear in "ana"
? Based on these return
values, you should be able to figure out how a given invocation of
the method should use the return value from the recursive call to
form its own return value.
I’m writing a recursive method that returns a value (for parts 3 and 4 in problem 8). My method works if I test it with parameters that constitute a base case, but it doesn’t work if a recursive call is required. Do you have any suggestions?
Make sure that you aren’t throwing away the return value of your recursive call. To avoid losing the return value, you can do one of two things:
Also, make sure that you are not trying to use a local variable to
accumulate a value across multiple invocations of the method. For
example, here is a faulty numOccur
method that we discussed in
lecture:
/* This version does NOT work! */ public static int numOccur(char ch, String str) { if (str == null || str.equals("")) { return 0; } int numOccur = 0; // every call gets its own numOccur if (str.charAt(0) == ch) { numOccur++; } numOccur(ch, str.substring(1)); return numOccur; }
It doesn’t work because each invocation of the method gets its own
copy of the numOccur
variable, and thus we can’t use that
variable to accumulate the number of occurrences.
Last updated on July 9, 2025.