Your logic sounds correct.
If you need help with the code you should read the API for String objects in Java.
String Documentation: String (Java Platform SE 6)

Especially the methods "length()",...