The PhoneClass is a Java class that provides functionality related to phone numbers. It reads phone numbers from a file, validates them, and performs various operations.
To read the Javadocs please visit https://slpixe.github.io/java-phone/
- Reading Phone Numbers from a File The readAllLinesAndStoreInField() method reads phone numbers from a file named fileTest.txt located in the src/main/resources directory. The phone numbers are stored in the lines field.
- Removing Comments The removeComments() method removes any lines starting with a hash (#) character. If any comments are removed, it returns true; otherwise, it returns false.
- Removing Empty Lines The removeEmptyLines() method removes empty lines from the list of phone numbers. If any empty lines are removed, it returns true; otherwise, it returns false.
- Number of Lines The numberOfLines() method returns the total number of phone numbers read from the file.
- Validating Phone Numbers The isNumberValid(String number) method checks if a given phone number matches the specified regex pattern. It returns true if the number is valid; otherwise, it returns false.
- Number of Valid and Invalid Phone Numbers
- The numberOfValidNumbers() method returns the count of valid phone numbers.
- The numberOfInvalidNumbers() method returns the count of invalid phone numbers.
- List of Valid Phone Numbers The listOfValidNumbers() method returns a list of valid phone numbers.
The regex pattern used for valid phone numbers is as follows:
(\\(\\d{3}\\)\\s{1}\\d{3}-{1}\\d{4})
(e.g., (123) 456-7890)(\\d{3}\\s{1}\\d{3}\\s{1}\\d{4})
(e.g., 123 456 7890)(\\d{3}-{1}\\d{3}-\\d{4})
(e.g., 123-456-7890)
These were tested on regex101 Regex101 - java phone number validation
public static void main(String[] args) {
PhoneClass phoneClass = new PhoneClass();
phoneClass.readAllLinesAndStoreInField();
System.out.println("Total number of lines: " + phoneClass.numberOfLines());
System.out.println("Number of valid phone numbers: " + phoneClass.numberOfValidNumbers());
System.out.println("Number of invalid phone numbers: " + phoneClass.numberOfInvalidNumbers());
List<String> validNumbers = phoneClass.listOfValidNumbers();
System.out.println("Valid phone numbers:");
validNumbers.forEach(System.out::println);
}
- Make sure to have maven installed e.g.
sdkman install maven
- Either run
mvn javadoc:javadoc
- or in Intellij select
Maven
->Lifecycle
->clean
(run) ->compile
(run)