Interesting problem. This could be either quick and dirty, or complex and complete. @Pete's approach is one simple way and may suffice for an assignment.
The ultimate and non-trivial way would be to have a
Java parser implemented with something like Antlr. Then analyze the parse tree. A Java language definition suitable to use as a spec that can be fed into Antlr can be found online but I've found those to sometimes be incomplete so you may end up searching for one that works. Then the analysis involves traversing the parse tree.
A middle of the road approach would involve first writing a filter that removed all comments and
string literals. You don't want to be counting occurrences that appear in those. This is trickier than it sounds but once written can be used in creating a variety of analysis tools.
Then find all words that begin with a lower case letter. This is not fool proof as Java compilers will accept variable names that begin with upper case letters. It is only by convention that variables begin with lower case letters.
Eliminate keywords.
Next, eliminate words that are immediately followed by an open parenthesis, those would be methods, not variables.
Next, keep track of scope. Variables defined within blocks ({...}), variables defined as method parameters, and class level variables.
See if you can detect assignment to a variable vs a declaration vs being used.