0

I have this program to write and have no idea how to control keyboard input:

Write a method to check whether the word entered is valid or not. Valid word should:

  1. Have at least 10 characters
  2. Start with a letter
  3. Contain a letter in upper case
  4. Contain at least 3 digits
  5. Contain a special character (e.g. @,$.% …etc)
  6. Contain a space
2
  • 2
    This isn't asking you to control keyboard, but to write a method that validates a String Commented Apr 8, 2014 at 20:51
  • You should take a look at String methods like charAt, length. Also some methods from Character class can be useful. If you want you can also write regex for that, but that could be an overkill. Commented Apr 8, 2014 at 20:53

3 Answers 3

2

You don't need to validate it on every key stroke, wait till the user has entered the word, then validate. Assuming you are using console input:

System.out.print("Enter something > ");
Scanner input = new Scanner(System.in);

String inputString = input.nextLine();

//perform validations on inputString, heres the first one:
//regex could be used instead of multiple if statements
if(inputString.length() < 10) {
    System.out.println("Validation failed, word was too short");
}
else if ... 
Sign up to request clarification or add additional context in comments.

4 Comments

not yet, you need to click the tick underneath the arrows
one more question if possible >>>> is there a method to check if there as an upper case in the word ?
@user3019158 that would be a good use of regular expressions.
As @ajb has said, regular expressions are useful for this. You can also use the technique described here
0

You can use this regular expression:

String REGEX = "(^[a-zA-Z](?=.*\\d{3,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%])(?=\\s+).{10,})";
String INPUT = "your password";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
System.out.println("matches(): "+matcher.matches());


^[a-zA-Z]           # Start with a letter
(?=.*\\d{3,})       # at least three digits must occur
(?=.*[a-z])         # a lower case letter must occur at least once
(?=.*[A-Z])         # an upper case letter must occur at least once
(?=.*[@#$%])        # a special character must occur at least once
(?=\\s+)            # a space must occur at least once
.{10,}              # anything, at least ten places though
$                   # end-of-string

To use this, read your password (from file,swing elements,...) and call a method, say validate(String pass), inside validate check it against the regex.

2 Comments

If you solve homework, you should do it right. The task is to write a method! ;)
@Absurd-Mind haha, I think that is the easy part. Nevertheless, I just updated my answer.
-1
Scanner input=new Scanner(System.in);
System.out.print("Enter Number");

//in here "input" is a Scanner name you can use any name for it as OBAMA.

int x=input.nextInt();

//here you can assigne the key board value for "x".

System.out.println(x);

then you can print it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.