Posts

Showing posts from September, 2024

Regex Pattern for Validating Length in Java 2024 - Java Technical Interview

Regex Pattern Program for Validating Length           Here we are going to check whether the given String has only certain number length using the Regex Pattern matching logic in Java. The steps are Logical Theory : Check for Length: Ensure the number has exactly 10 digits. Regex Pattern for Validation :      1. Regex: ^.{10}$      2. Explanation:               ^ asserts the start of the string.               .{10} matches exact length10.               $ asserts the end of the string. Code : package com.dinatechy.challange; import java.util.regex.Matcher; import java.util.regex.Pattern; public class LenghtPattern { public static void main(String[] args ) { // Pattern for validate length String content = "DinaTechy" ; // length - 9 Pattern pattern = Pattern. compile ( "^.{9}$" ); Matcher matcher ...

Regex Pattern for Mobile Number in Java 2024 - Java Technical Interview

 Regex Pattern for Mobile Number     Here we are going to check whether the given String is contain mobile number or not using the Regex Pattern matching logic in Java. The steps are Logical Theory : Check for Length : Ensure the number has exactly 10 digits (or more if including a country code). Check for Digits : Ensure that all characters are numeric. Regex Pattern for Validation : Regex: ^\d{10}$ Explanation: ^ asserts the start of the string. \d{10} matches exactly 10 digits. $ asserts the end of the string. Code : package com.dinatechy.challange; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MobileNoPattern { public static void main(String[] args ) { // Mobile number Pattern // length should be 10 // must be digits String mobileNo = "9876543210" ; Pattern pattern = Pattern. compile ( "^\\d{10}$" ); // d refers to only digit's // 10 length Matcher matcher = pattern .matcher( mobileN...

Finding Triangle with Three lengths in Java 2024 - Java Technical Interview

 Finding Triangle with Three length          We are going to find which Triangle's side this are using Java. 1. We are check three Triangle, which is  Equilateral, Isosceles and Scalene. 2. First we need to know the Mathmetical logic for all this,      Equilateral ==> all the three length's are equal.      Isosceles ==> either two side's are equal      Scalene ==> all three side's are different 3. Final part is we need to implement this in code. Code :  package com.dinatechy.challange; public class FindWhichTriangle { public static void main(String[] args ) { // Find the Which Triangle based on the given Three length's // Equilateral == all the three lenght's are equal, // Isosceles == either two side's are equal or // Scalene == all three side's are different int a = 23, b = 231, c = 232; if ( a == b && b == c && c == a ) Syste...

Pattern Matching in Java - Technical Interview 2024 - Problem Solving

 Pattern Matching Logic               First thing we need to know the content we are going to do the matching and the pattern. 1. Create a reference for Pattern class and then Call the Pattern class static method compile method and send the parameter as pattern you want to search for. 2. Create reference for Matcher and assign the method which is Pattern class method matcher, here send the parameter as content we need to check. 3. Finally call the find method in Matcher class to check the pattern is matched or not. If the method is return true the pattern is matched, if the method return false the pattern is not matched. Code :  package com.dinatechy.challange; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternMatching { public static void main(String[] args ) { // Pattern Matching in Java // Example : content = "Hello people hi ", pattern = " hi " // Regex String content = ...