Java Articles

Page 12 of 450

What is Java reg ex to check for date and time?

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

To match a regular expression with the given string You need to:.Compile the regular 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.The matches() method of the Matcher class returns true if a match occurs else it returns false. Therefore, invoke this method to validate the data.ExampleFollowing is a Java regular expression example matches only dateimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Sample {    public static void main(String args[]){       //Creating the list to store ...

Read More

How to create Directories using the File utility methods in Java?

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

Since Java 7 the File.02s class was introduced this contains (static) methods that operate on files, directories, or other types of files.The createDirectory() method of the Files class accepts the path of the required directory and creates a new directory.ExampleFollowing Java example reads the path and name of the directory to be created, from the user, and creates it.import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class Test {    public static void main(String args[]) throws IOException {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);       ...

Read More

How to convert a super class variable into a sub class type in Java

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

Inheritance is a relation between two classes where one class inherits the properties of the other class. This relation can be defined using the extends keyword as −public class A extends B{}The class which inherits the properties is known as sub class or, child class and the class whose properties are inherited is super class or, parent class.In inheritance a copy of super class members is created in the sub class object. Therefore, using the sub class object you can access the members of the both classes.Converting a super class reference variable into a sub class typeYou can try to convert ...

Read More

Find the Intersection Point of Two Linked Lists in Java

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 711 Views

A Linked List is a linear data structure in which each node has two blocks such that one block contains the value or data of the node and the other block contains the address of the next field.Let us assume that we have a linked list such that each node contains a random pointer which is pointing to the other nodes in the list. The task is to find the node at which two linked lists intersect each other. If they don’t intersect, then return NULL or empty as output.For ExampleInput-1:Output:2Explanation: Since the given linked list intersects at the node with ...

Read More

Find the Nth Ugly Number in Java

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 1K+ Views

A number whose prime factors are either 2, 3 or 5 is called an Ugly Number.  Some of the ugly numbers are: 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, etc.We have a number N and the task is to find the Nth Ugly number in the sequence of Ugly numbers.For Example:Input-1:N = 5Output:5Explanation:The 5th ugly number in the sequence of ugly numbers [1, 2, 3, 4, 5, 6, 8, 10, 12, 15] is 5.Input-2:N = 7Output:8Explanation:The 7th ugly number in the sequence of ugly numbers [1, 2, 3, 4, 5, 6, 8, 10, 12, 15] is 8.Approach ...

Read More

Count occurrences of a substring recursively in Java

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 9K+ Views

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive process.A recursive function is the one which has its own call inside it’s definition.If str1 is “I know that you know that i know” str2=”know”Count of occurences is − 3Let us understand with examples.For ExampleInputstr1 = "TPisTPareTPamTP", str2 = "TP";OutputCount of occurrences of a substring recursively are: 4ExplanationThe substring TP occurs 4 times in str1.Inputstr1 = "HiHOwAReyouHiHi" str2 = "Hi"OutputCount of occurrences of a substring recursively are: 3ExplanationThe substring Hi occurs 3 times in str1.Approach used ...

Read More

Parent and Child classes having same data member in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 12K+ Views

The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.ExampleFollowing is an example −class Demo_base {    int value = 1000;    Demo_base() {       System.out.println("This is the base class constructor");    } } class Demo_inherits extends Demo_base {    int value = 10;    Demo_inherits() {       System.out.println("This is the inherited class ...

Read More

Quantifiers in Java

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 356 Views

Quantifier is a concept that allows the programmer to specify the number of occurrences of a specific type of value in the regular expression. There are different types of quantifiers, some of them include ‘?’ (Reluctant quantifier), ‘+’ (Possessive quantifier). In this post, we will see how reluctant quantifier works.ExampleFollowing is an example −import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo {    public static void main(String[] args) {       Pattern my_pattern = Pattern.compile("sam+?");       Matcher my_match = my_pattern.matcher("samp");       while (my_match.find())       System.out.println("The pattern has been found - " + my_match.start() + ...

Read More

Retrieving Elements from Collection in Java- For-each loop

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 364 Views

The ‘for-each’ loop is used to iterate over a set of elements that is stored in a data structure.Syntaxfor (element e: collection) {    System.out.println(e); }ExampleFollowing is an example −public class Demo {    public static void main(String[] args) {       int[] my_vals = {5, 67, 89, 31, -1, 2, 0};       int sum = 0;       for (int number: my_vals) {          sum += number;       }       System.out.println("The sum is " + sum);    } }OutputThe sum is 193A class named Demo contains the main ...

Read More

Retrieving Elements from Collection in Java- Iterator

AmitDiwan
AmitDiwan
Updated on 11-Mar-2026 426 Views

Following is an example to retrieve elements −Exampleimport java.util.*; public class Demo {    public static void main(String args[]) {       HashSet my_hs = new HashSet() ;       my_hs.add("Joe");       my_hs.add ("Rob");       Iterator my_it = my_hs.iterator();       System.out.println("The elements are : ");       while (my_it.hasNext())          System.out.println(my_it.next());    } }OutputThe elements are : Joe RobA class named Demo contains the main function, which defines a HashSet collection. Elements are added to this collection using the ‘add’ function. An iterator is defined, and the elements ...

Read More
Showing 111–120 of 4,495 articles
« Prev 1 10 11 12 13 14 450 Next »
Advertisements