Posted by Lisa on .
I have to prompt a user to input a password and have to verify whether or not it is a single word. How would I do something like?
Computer Science-Java -
You would read the password and store it as as String. Then there are many ways to check if it is a single word.
You need to know what the separators of words are. Most of the time we think of spaces, but there are tabs, punctuation marks, parentheses, etc.
If it is a single character (space) you can use the string function indexOf(String, character) to see if it returns a non negative number.
If it is a collection of different characters, you may think of using the string function matches(regexp) which allows you to specify many characters at the same time.
If you read up the String class of the Java API, you will find a lot of possibilities.
There is even a video on Strings on UTube (I have not perused it):