Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 5 of 50

Java regular expression program to validate an email including blank field valid as well

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 346 Views

Following regular expression matches given e-mail id including the blank input −^([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2, 6})?$Where, ^ matches the starting of the sentence.[a-zA-Z0-9._%+-] matches one character from English alphabet (both cases), digits, "+", "_", ".", "" and, "-" before the @ symbol.+ indicates the repetition of the above mentioned set of characters one or more times.@ matches itself[a-zA-Z0-9.-] matches one character from English alphabet (both cases), digits, "." and "-" after the @ symbol\.[a-zA-Z]{2, 6} two to 6 letter for email domain after "."$ indicates the end of the sentenceExample 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest {    public static ...

Read More

List.replaceAll(UnaryOperator operator) method in Java

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation, performs the specified operation on all the elements of the current list and replaces the existing values in the list with their respective results.Exampleimport java.util.ArrayList; import java.util.function.UnaryOperator; class Op implements UnaryOperator {    public String apply(String str) {       return str.toUpperCase();    } } public class Test {    public static void main(String[] args) throws CloneNotSupportedException {       ArrayList list = new ArrayList();       list.add("Java");       list.add("JavaScript");       list.add("CoffeeScript");       list.add("HBase"); ...

Read More

Java regex program to split a string at every space and punctuation.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

The regular expression "[!._, '@?//s]" matches all the punctuation marks and spaces.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main( String args[] ) {       String input = "This is!a.sample"text, with punctuation!marks";       Pattern p = Pattern.compile("[!._, '@?//s]");       Matcher m = p.matcher(input);       int count = 0;       while(m.find()) {          count++;       }       System.out.println("Number of matches: "+count);    } }OutputNumber of matches: 8The split() method of the String class accepts a value representing a ...

Read More

How to print all the characters of a string using regular expression in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

The meta character "." matches all the characters, to print all the characters using the regular expressions −Compile the regular expression using the compile() method.Create a Matcher object using the matcher() method.Find the matches using the find() method and for every match print the matched contents (characters) using the group() method.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       //Regular expression to match a string of non-word with length 2 to 6       String regex = ".";       Scanner sc = new Scanner(System.in); ...

Read More

Posix character classes p{Lu} Java regex

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 333 Views

This class \p{Lu} matches upper case letters.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example1 {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "\p{Lu}";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0;   ...

Read More

MatchResult end(int group) method in Java with examples.

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 158 Views

The java.util.regex.MatcheResult interface provides methods to retrieve the results of a match.You can get an object of this interface using the toMatchResult() method of the Matcher class. This method returns a MatchResult object which represents the match state of the current matcher.The end(int group) method of this interface accepts an integer representing a particular group and returns the offset after the last match occurred in the specified group.Exampleimport java.util.Scanner; import java.util.regex.MatchResult; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       String regex = "(.*)(\d+)(.*)";       //Reading input ...

Read More

How to copy a specific section of an array in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 526 Views

Using copyOf() methodThe copyOf() method of the Arrays class (java.util package) accepts two parameters −an array (of any type).an integer value representing length.And copies the contents of the given array from starting position to given length and returns the new array.Exampleimport java.util.Arrays; public class CopyingSectionOfArray {    public static void main(String[] args) {       String str[] = new String[10];       //Populating the array       str[0] = "Java";       str[1] = "WebGL";       str[2] = "OpenCV";       str[3] = "OpenNLP";       str[4] = "JOGL";       ...

Read More

How many ways are there to initialize the instance variables of a class in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 4K+ Views

You can initialize the instance variables of a class using final methods, constructors or, Instance initialization blocks.Final methodsWhenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass’s final method from the subclass.i.e. The purpose of making a method final is to prevent modification of a method from outside (child class). You can also use these final methods to initialize the instance variables.Exampleimport java.util.Scanner; public class FinalMethods {    int age = getAge();    String name = getName();    static Scanner sc = new Scanner(System.in);    public static final int getAge() {   ...

Read More

How to access the fields of an interface in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 15K+ Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface. By default, All the members (methods and fields) of an interface are public.All the methods in an interface are public and abstract (except static and default).All the fields of an interface are public, static and, final by default.If you declare/define fields without public or, static or, final or, all the three modifiers. Java compiler places them on your behalf.ExampleIn the following Java program, ...

Read More

Is it possible to instantiate Type-parameter in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 5K+ Views

Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.Exampleclass Student {    T age;    Student(T age){       this.age = age;    }    public void display() {       System.out.println("Value of age: "+this.age);    } } public class GenericsExample { ...

Read More
Showing 41–50 of 500 articles
« Prev 1 3 4 5 6 7 50 Next »
Advertisements