How can I check the format of string input

Hunter Stockdill

I'm not sure if I'm checking in the wrong area or if my code to check is wrong. I am attempting to check if employee Id is valid in format 999-A. Any help would be appreciated.

    public class Employee {
  private String name;
  private String employeeNumber;
  private String hireDate;

  // Constructor
  public Employee(String name, String employeeNumber, String hireDate){

    this.name = name;

    if(isValidEmployeeNumber(employeeNumber) == true){
      this.employeeNumber = employeeNumber;
    } else{

    }
    this.hireDate = hireDate;

  }

  // Getters and Setters

  public String getName(){
    return name;
  }

  public String getEmployeeNumber(){
    return employeeNumber;
  }

  public String getHireDate(){
    return hireDate;
  }

  public void setName(String name){
    this.name = name;
  }

  public void setEmployeeNumber(String employeeNumber){
    this.employeeNumber = employeeNumber;
  }

  public void setHireDate(String hireDate){
    this.hireDate = hireDate;
  }

  //999-X
  private boolean isValidEmployeeNumber(String employeeNumber){

    if(Character.getNumericValue(employeeNumber.charAt(4)) > 10 &
        Character.getNumericValue(employeeNumber.charAt(4)) < 22)
    {
      /*
      int cutNum = Integer.parseInt(employeeNumber.substring(0,1));

      if(cutNum <= 0 && cutNum <= 9){

        cutNum = Integer.parseInt(employeeNumber.substring(1, 2));

        if(cutNum <= 0 && cutNum <= 9){
          cutNum = Integer.parseInt(employeeNumber.substring(2, 3));

          if(cutNum <= 0 && cutNum <= 9){

              setEmployeeNumber(employeeNumber);
              return true;

          }

        }

      }*/

      return true;

    }

    return false;
  }

}

As you can see I commented out the part that I have attempted even when commenting it out the employeeNumber is coming out null. I have attempted string.matches("\d{1}\d{1}\d{1}\-\D{1}") and a few other strategies.

beny23

Regular expressions are a very good fit for this task:

private boolean isValidEmployeeNumber(String employeeNumber) {
    return employeeNumber.matches("[0-9]{3}-[A-Z]")
}

To break down the regular expression:

  • [0-9]{3} a number (0-9) 3 times
  • - the dash
  • [A-Z] A-Z once

anything else wouldn't be valid. Note \D would be valid for any non-number, which I don't think what you intended.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I check if an input is a integer or String, etc.. in JAVA?

From Dev

How can i check if input is a number ? If its a string alert an error

From Dev

How to check if input string matches a specific format?

From Dev

How can I verify an input parameter format?

From Dev

How can I check if the input field is not empty

From Dev

How to format Date Input that only the format I want can continue

From Dev

How can I check for invalid input and loop until the input is valid?

From Dev

How can I escape the format string?

From Dev

How Can I Correctly Format a String In Java?

From Dev

How can i check if i have a particular format

From Dev

How can I check the last letter of a string?

From Dev

How can I check if a string is available in a table?

From Dev

How can I check the position of \"results\":[{ in a string?

From Dev

How can I check if a string is a number in Python?

From Dev

How can I take a string input like this?

From Dev

How can I add a format specifier to `format-time-string`?

From Dev

How can I use a dynamic format string with the format! macro?

From Dev

How can I check the ruby format for a bunch of files and/or a single file?

From Dev

How can I check the video/audio format for a MKV file?

From Dev

How do I take string date input and convert it into another format?

From Dev

how do i check for a string occurence in input data

From Dev

How would I check whether or not there is a string in an input - beginner

From Dev

How can I specify the format of AVAudioEngine Mic-Input?

From Dev

How can I dynamically format an input element in angular2?

From Dev

How can I check for keyboard input without stopping execution?

From Dev

How can I check if the user input with the database information?

From Dev

How can I check if the text of an input field is "yes" or "no"?

From Dev

How can I check if my function is called with input from a pipe?

From Dev

How can I check if the user input is a word and not a special character like (*!?)?

Related Related

  1. 1

    How can I check if an input is a integer or String, etc.. in JAVA?

  2. 2

    How can i check if input is a number ? If its a string alert an error

  3. 3

    How to check if input string matches a specific format?

  4. 4

    How can I verify an input parameter format?

  5. 5

    How can I check if the input field is not empty

  6. 6

    How to format Date Input that only the format I want can continue

  7. 7

    How can I check for invalid input and loop until the input is valid?

  8. 8

    How can I escape the format string?

  9. 9

    How Can I Correctly Format a String In Java?

  10. 10

    How can i check if i have a particular format

  11. 11

    How can I check the last letter of a string?

  12. 12

    How can I check if a string is available in a table?

  13. 13

    How can I check the position of \"results\":[{ in a string?

  14. 14

    How can I check if a string is a number in Python?

  15. 15

    How can I take a string input like this?

  16. 16

    How can I add a format specifier to `format-time-string`?

  17. 17

    How can I use a dynamic format string with the format! macro?

  18. 18

    How can I check the ruby format for a bunch of files and/or a single file?

  19. 19

    How can I check the video/audio format for a MKV file?

  20. 20

    How do I take string date input and convert it into another format?

  21. 21

    how do i check for a string occurence in input data

  22. 22

    How would I check whether or not there is a string in an input - beginner

  23. 23

    How can I specify the format of AVAudioEngine Mic-Input?

  24. 24

    How can I dynamically format an input element in angular2?

  25. 25

    How can I check for keyboard input without stopping execution?

  26. 26

    How can I check if the user input with the database information?

  27. 27

    How can I check if the text of an input field is "yes" or "no"?

  28. 28

    How can I check if my function is called with input from a pipe?

  29. 29

    How can I check if the user input is a word and not a special character like (*!?)?

HotTag

Archive