Found 7442 Articles for Java

Difference between Scanner and BufferReader Class in Java

Ashin Vincent
Updated on 16-Apr-2025 15:51:50

12K+ Views

Scanner and BufferedReader classes are used to read input from an external system. Scanner is normally used when we know input is of type string or of primitive types, and BufferedReader is used to read text from character streams while buffering the characters for efficient reading of characters. What is Scanner Class? The Scanner class is included in the java.util package. It is mostly used when the data type of the input is already known. We commonly use it with data ty+pes like strings, integers, floats, and booleans. It has built-in methods like nextInt(), nextDouble(), and nextLine() that help ... Read More

Differences between abstract class and concrete class in Java

Ashin Vincent
Updated on 16-Apr-2025 15:48:22

12K+ Views

Abstract class and concrete class are fundamental concepts of object oriented programming in Java. In this article, we will learn the differences between an abstract class and concrete class. What is an Abstract Class? An abstract class is a class that cannot be used to create objects. It can only be accessed using its subclasses. It can contain abstract methods, which are methods without a body. It acts as a blueprint for its subclasses. It can also contain concrete or regular methods. Example This example shows how to implement an abstract class in java: ... Read More

Posix character classes p{Upper} Java regex

Maruthi Krishna
Updated on 21-Nov-2019 08:16:31

269 Views

This class matches the upper case alphabetic characters.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Posix_LowerExample {    public static void main( String args[] ) {       //Regular expression to match upper case letters       String regex = "^\p{Upper}+$";       //Getting the input data       Scanner sc = new Scanner(System.in);       System.out.println("Enter 5 input strings: ");       String input[] = new String[5];       for (int i=0; i

How match a string irrespective of case using Java regex.

Maruthi Krishna
Updated on 21-Nov-2019 08:06:13

241 Views

The compile method of the patter class accepts two parameters −A string value representing the regular expression.An integer value a field of the Pattern class.This CASE_INSENSITIVE field of the Pattern class matches characters irrespective of case. Therefore, if you pass as flag value to the compile() method along with your regular expression, characters of both cases will be matched.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example {    public static void main( String args[] ) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input data: ");       String input = sc.nextLine(); ... Read More

How to extract numbers from a string using regular expressions?

Maruthi Krishna
Updated on 21-Nov-2019 08:03:39

16K+ Views

You can match numbers in the given string using either of the following regular expressions −“\d+” Or, "([0-9]+)"Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExtractingDigits {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter sample text: ");       String data = sc.nextLine();       //Regular expression to match digits in a string       String regex = "\d+";       //Creating a pattern object       Pattern pattern = Pattern.compile(regex);       //Creating a Matcher object       Matcher ... Read More

How to replace multiple spaces in a string using a single space using Java regex?

Maruthi Krishna
Updated on 21-Nov-2019 07:59:21

13K+ Views

The metacharacter “\s” matches spaces and + indicates the occurrence of the spaces one or more times, therefore, the regular expression \S+ matches all the space characters (single or multiple). Therefore, to replace multiple spaces with a single space.Match the input string with the above regular expression and replace the results with single space “ ”.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAllExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a String");       Scanner sc = new Scanner(System.in);       String input = ... Read More

How to extract each (English) word from a string using regular expression in Java?

Maruthi Krishna
Updated on 21-Nov-2019 07:57:30

1K+ Views

The regular expression “[a-zA-Z]+” matches one or the English alphabet. Therefore, to extract each word in the given input string −Compile the above expression of the compile() method of the Pattern class.Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.Finally, for each match get the matched characters by invoking the group() method.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EachWordExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter sample text: ");       String data = ... Read More

Checking for valid email address using regular expressions in Java

Maruthi Krishna
Updated on 19-Feb-2024 04:14:12

81K+ Views

To verify whether a given input string is a valid e-mail id match it with the following is the regular expression to match an e-mail id −"^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$"Where, ^ matches the starting of the sentence.[a-zA-Z0-9+_.-] matches one character from the 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 the English alphabet (both cases), digits, "." and "–" after the @ symbol.$ indicates the end of the sentence.Exampleimport java.util.Scanner; public class ValidatingEmail {    public static void ... Read More

Program to check valid mobile number using Java regular expressions

Maruthi Krishna
Updated on 21-Nov-2019 07:50:55

5K+ Views

You can match a valid mobile number using the following regular expression −"\d{10}"A valid mobile number generally have 10 digits (in India).The metacharacter "\d" matches the digits from 0 to 9.The quantifier ex{n} suggests the repetition of ex n times.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PhoneNumberExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter your name: ");       String name = sc.nextLine();       System.out.println("Enter your Phone number: ");       String phone = sc.next();       //Regular expression to ... Read More

Java Regular expression to check if a string contains alphabet

Maruthi Krishna
Updated on 21-Nov-2019 07:46:41

5K+ Views

Following is the regular expression to match alphabet in the given input −"^[a-zA-Z]*$"Where,^ matches the starting of the sentence.[a-zA-z] matches the lower case and upper case letters.* indicates the occurrence for zero or more times.& indicates the end of the line.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ContainsAlphabetExample {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       String names[] = new String[5];       for(int i=0; i

Advertisements