Wednesday
March 29, 2017

Post a New Question

Posted by on .

Hi,

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.
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html
    There is even a video on Strings on UTube (I have not perused it):
    http://www.youtube.com/watch?v=tU67suVZVfQ

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question