Question

JAVA Q The criteria for a valid simple username is as follows: It should start with...

JAVA Q

The criteria for a valid simple username is as follows:

  • It should start with an English alphabet followed by alphanumeric characters.
  • No special characters allowed
  • Username length should be at least 3 and should not exceed 20.

You are required to complete the regular expression only. Your answer will be tested automatically.

For example: the following are valid username:

  • abc001
  • davidsmith

And the following are invalid username:

  • 12helloworld"
  • 336.12.1.2
Test Result
isUsername("abc001");
isUsername("128.1.1");
abc001: matches
128.1.1: does not match

public static void isUsername(String inputStr) {
String regExpStr = ""; //complete this
Pattern pattern = Pattern.compile(regExpStr);
Matcher matcher = pattern.matcher(inputStr);
System.out.printf("%s\n", matcher.matches()? inputStr + ": matches": inputStr + ": does not match");
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java code

============================================================================================

import java.util.regex.*;


public class UserNameValidation {

   public static void isUsername(String inputStr) {
       String regExpStr = "^[a-zA-Z][a-zA-Z0-9]{3,20}$"; //complete this
       Pattern pattern = Pattern.compile(regExpStr);
       Matcher matcher = pattern.matcher(inputStr);
       System.out.printf("%s\n", matcher.matches()? inputStr + ": matches": inputStr + ": does not match");
       }
  
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       isUsername("abc001");
       isUsername("128.1.1");
       isUsername("128aa");
       isUsername("aa");
   }

}

============================================================================================

Output

Add a comment
Know the answer?
Add Answer to:
JAVA Q The criteria for a valid simple username is as follows: It should start with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Written in Java I have an error in the accountFormCheck block can i get help to...

    Written in Java I have an error in the accountFormCheck block can i get help to fix it please. Java code is below. I keep getting an exception error when debugging it as well Collector.java package userCreation; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.regex.Matcher; import java.util.regex.Pattern; import javafx.event.ActionEvent; import javafx.fxml.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.text.Text; import javafx.stage.*; public class Controller implements Initializable{ @FXML private PasswordField Password_text; @FXML private PasswordField Confirm_text; @FXML private TextField FirstName_text; @FXML private TextField LastName_text;...

  • This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions...

    This Exercise contains two classes: ValidateTest and Validate. The Validate class uses try-catch and Regex expressions in methods to test data passed to it from the ValidateTest program. Please watch the related videos listed in Canvas and finish creating the 3 remaining methods in the class. The regular expression you will need for a Phone number is: "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$" and for an SSN: "^\\d{3}[- ]?\\d{2}[- ]?\\d{4}$" This exercise is meant to be done with the video listed in the...

  • You will be writing a simple Java program that implements an ancient form of encryption known...

    You will be writing a simple Java program that implements an ancient form of encryption known as a substitution cipher or a Caesar cipher (after Julius Caesar, who reportedly used it to send messages to his armies) or a shift cipher. In a Caesar cipher, the letters in a message are replaced by the letters of a "shifted" alphabet. So for example if we had a shift of 3 we might have the following replacements: Original alphabet: A B C...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • I have Majority of the code written but I just need to implement the <,>,and =...

    I have Majority of the code written but I just need to implement the <,>,and = sign in the code. I have posted the instructions for the whole code. I will add what I have at the end. Objectives: Implement basic class concepts in Java Implement inheritance with super and sub-classes Implement basic polymorphism concepts Problem: The TARDIS has been infected by a virus which means it is up to Doctor Who to manually enter calculations into the TARDIS interface....

  • 60 points, Complete javadocs documentation required Be sure to submit all files (.java and dictio...

    60 points, Complete javadocs documentation required Be sure to submit all files (.java and dictionary.txt) required to run your program Background Boggle is a word game using a plastic grid of lettered dice, in which players attempt to find words in sequences of adjacent letters. The dice are randomly arranged in the grid, and players have 90 seconds to form as many words as possible from adjacent top-facing letters For example, the word SUPER is spelled in the gameboard to...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • please help me out and input values please .. main cpp.. please follow intructions exactly witj...

    please help me out and input values please .. main cpp.. please follow intructions exactly witj clarity please CSE 110-Intro to Computer Programming Project #3 Requirements-String Class Functions and Test Case Documentation Using the String Class, design and develop a multi-file password validation program and Test case Document (10% of grade) that requests a password, and validates it based on the following criteria. The password must be at least 8 characters long, contain at least one number, and at least...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • **** ITS MULTI-PART QUESTION. PLEASE MAKE SURE TO ANSWER THEM ALL. SOLVE IT BY JAVA. ****...

    **** ITS MULTI-PART QUESTION. PLEASE MAKE SURE TO ANSWER THEM ALL. SOLVE IT BY JAVA. **** *** ALSO MAKE SURE IT PASS THE TESTER FILE PLEASE*** Often when we are running a program, it will have a number of configuration options which tweak its behavior while it's running. Allow text completion? Collect anonymous usage data? These are all options our programs may use. We can store these options in an array and pass it to the program. In its simplest...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT