Maruthi Krishna has Published 870 Articles

Regular Expression "^" (caret) Metacharacter in Java

Maruthi Krishna

Maruthi Krishna

Updated on 18-Nov-2019 10:12:52

657 Views

The subexpression/metacharacter “^” matches the beginning of a line. If you use this in a regular expression, it matches the sentence succeeding it in the input string.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample {    public static void main( String args[] ) {       String regex = ... Read More

Remove Leading Zeroes from a String in Java using regular expressions

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 12:51:11

8K+ Views

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.Following is the regular expression to match the leading zeros of a string −The ^0+(?!$)";To remove the leading zeros from a string pass this as ... Read More

What is ArrayStoreException in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 10:44:49

614 Views

When you have created an array of a particular data type with fixed size and populate it if you store a value other than its datatype an ArrayStoreException is thrown at the run time.ExampleIn the following Java program, we are creating an Integer array and trying to store a double ... Read More

How to Get a slice of a primitive array in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 09:25:21

897 Views

You can get a part of a Java array in between two specified indexes in various ways.By Copying contents:One way to do so is to create an empty array and copy the contents of the original array from the start index to the endIndex.Example Live Demoimport java.util.Arrays; public class SlicingAnArray { ... Read More

Converting String to StringBuilder in Java

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:38:52

643 Views

The append() method of the StringBuilder class accepts a String value and adds it to the current object.To convert a String value to StringBuilder object −Get the string value.Append the obtained string to the StringBuilder using the append() method.ExampleIn the following Java program, we are converting an array of Strings ... Read More

Removing leading zeros from a String using apache commons library in Java

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:31:57

2K+ Views

The stripStart() method of the org.apache.commons.lang.StringUtils class accepts two strings and removes the set of characters represented by the second string from the string of the first string.To remove leading zeros from a string using apache communal library −Add the following dependency to your pom.xml file    org.apache.commons    commons-lang3 ... Read More

How to make a collection thread safe in java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 07:08:23

2K+ Views

The Collections class of java.util package methods that exclusively work on collections these methods provide various additional operations which involves polymorphic algorithms.This class provides different variants of the synchronizedCollection() method as shown below −Sr.NoMethods & Description1static Collection synchronizedCollection(Collection c)This method accepts any collection object and, returns a synchronized (thread-safe) ... Read More

How to make elements of array immutable in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 15-Oct-2019 06:44:33

3K+ Views

No, you cannot make the elements of an array immutable.But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.And the asList() ... Read More

How to resolve "Could not find or load main class package" in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 14-Oct-2019 07:59:08

5K+ Views

Once you write a Java program you need to compile it using the javac command, this shows you the compile time errors occurred (if any).Once you resolve them and compile your program success fully, an executable file with the same name as your class name is generated in your current ... Read More

Program to replace all the characters in of a file with '#' except a particular word in Java

Maruthi Krishna

Maruthi Krishna

Updated on 14-Oct-2019 07:51:55

619 Views

The split() method of the String class. splits the current string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.The ... Read More

Advertisements