Reputation: 1459
I'm doing homework and I have to rewrite the StringTokenizer class in Java. When the nextToken()
method is called and there are no more tokens my method should return a value of null. My homework is complete except this part. I don't understand what exactly that means and what I'm supposed to do.
I've searched this website for answers but really don't understand the answers or examples given. My question is how does null fit into the methods nextToken()
or hasMoreTokens()
in the StringTokenizer
class in Java?
Some of the other posts on this subject that I've looked at. What is null in Java?
Upvotes: 1
Views: 65
Reputation: 234797
Since nextToken()
returns a String
, all you need to do when there are no more tokens is:
return null;
You can use the keyword null
as the value for any Java Object
. It simply means "no object".
This won't work for hasMoreTokens
, which is supposed to return a boolean
value (either true
or false
). Primitive values (boolean
, int
, etc.) are not objects, you cannot use null
as a return value in those situations.
Upvotes: 7