Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 25 of 450
Named captured groups Java regular expressions
Named capturing groups allows you to reference the groups by names. Java started supporting captured groups since SE7.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReplaceAll{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter input text: "); String input = sc.nextLine(); String regex = "(?[\d]{2})-(?[\d]{5})-(?[\d]{6})"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(input); while (matcher.find()) { ...
Read MoreMatching multiple lines in Java regular expressions
To match/search a input data with multiple lines −Get the input string.Split it into an array of tokens by passing "\r?" as parameter to the split method.Compile the required regular expression using the compile() method of the pattern class.Retrieve the matcher object using the matcher() method.In the for loop find matches in the each element (new line) of the array using the find() method.Reset the input of the matcher to the next element of the array using the reset() method.Exampleimport java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchingText{ public static void main(String[] args) { String input = "sample ...
Read MorePrint all permutation of a string using ArrayList in Java
In this problem, we are given a string of size n and we have to print all permutations of the string. But this time we have to print this permutation using ArrayList.Let’s take an example to understand the problem -Input − string = ‘XYZ’Output − XYZ, XZY, YXZ, YZX, ZXY, ZYXTo solve this problem, we will be generating all permutations of the character of the string. We will use a recursive function and will return arrayList.ExampleThe following is ArrayList implementation of the algorithm −import java.util.ArrayList; public class Main{ static void printArrayList(ArrayList combo) { combo.remove(""); ...
Read MoreJava regex program to verify whether a String contains at least one alphanumeric character.
Following regular expression matches a string that contains at least one alphanumeric characters −"^.*[a-zA-Z0-9]+.*$";Where, ^.* Matches the string starting with zero or more (any) characters.[a-zA-Z0-9]+ Matches at least one alpha-numeric character..*$ Matches the string ending with zero or more (ant) characters.Example 1import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { 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 = "^.*[a-zA-Z0-9]+.*$"; ...
Read MoreBinaryOperator Interface in Java
The BinaryOperator interface represents an operation upon two operands of the same type, producing a result of the same type as the operands.Following are the methods −Modifier and TypeMethod and DescriptionmaxBy(Comparator
Read MoreJava regex program to add space between a number and word in Java.
You can form matched groups in the regular expression by separating the expressions with parenthesis. In following regular expression the first group matches digits and the second group matches the English alphabet −(\d)([A-Za-z])In short, it matches the part in the input string where a digit followed by an alphabet.Since the expression $1 indicates Group1 and $2 indicates Group2, if you replace The above Java regular expression with $1 $2, using the replace() method (of the String class) a space will be added between number and a word in the given input string when a number is followed by a word.Exampleimport ...
Read MoreHow to get Exception log from a console and write it to external file in java?
There are several logging frame works available to log your data in to files. You can also define your own method.Example − Using I/O packageFollowing Java program has an array storing 5 integer values, we are letting the user to choose two elements from the array (indices of the elements) and performing division between them. We are wrapping this code in try block with three catch blocks catching ArithmeticException, InputMismatchException and, ArrayIndexOutOfBoundsException. In each of them we are invoking the writeToFile() method.This method accepts an exception object, and appends it to a file using the write() method of the Files ...
Read MoreCan we to override a catch block in java?
DescriptionWhen a piece of code in particular method throws an exception, and is handled using try-catch pair. If we are calling this method from another one and, the calling line is wrapped within try-catch pair. Now, how can I override the catch block by the catch block of the calling method.When a piece of code in a method throws an exception (compile time) we must either handle it by wrapping it within the try-catch pair or, throw it (postpone) to the calling method using the throws keyword else a compile time error occurs.In the following Java example the code in ...
Read MoreOut of memory exception in Java:\\n
Whenever you create an object in Java it is stored in the heap area of the JVM. If the JVM is not able to allocate memory for the newly created objects an exception named OutOfMemoryError is thrown.This usually occurs when we are not closing objects for long time or, trying to act huge amount of data at once.There are 3 types of errors in OutOfMemoryError −Java heap space.GC Overhead limit exceeded.Permgen space.Example 1public class SpaceErrorExample { public static void main(String args[]) throws Exception { Float[] array = new Float[10000 * 100000]; } }OutputRuntime exceptionException in ...
Read MoreChecked Vs unchecked exceptions in Java programming.
Checked exceptionsA checked exception is an exception that occurs at the compile time, these are also called as compile-time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after the execution of the complete program.Exampleimport java.io.File; import java.io.FileInputStream; public class Test { public static void main(String args[]){ System.out.println("Hello"); try{ ...
Read More