tweaked the tojava method
finished to be the same as sample code