How can a String be validated (for alphabets) in java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:45:51

5K+ Views

Problem Statement The given problem is to validate a string to check if that string contains only alphabets (both uppercase and lowercase) and does not contains any other characters like numbers, special characters, etc.. For example, the string "HelloWorld" is valid as it contains only alphabets, while "Hello123" is not valid as it contains numbers. Solution We can solve this problem by using following methods: Using Regular Expressions Manual Checking using loop Using Character.isAlphabetic() Using Regular Expressions We can use regular expressions to validate a string for alphabets. The regular expression ^[a-zA-Z]+$ matches a string that contains only ... Read More

How to create a variable that can be set only once but isn't final in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:43:48

2K+ Views

How to create a variable that can be set only once, meaning it can be assigned a value only once but is not final in Java? Solution In Java we can solve the above using the following two methods: Using a custom wrapper class Using an AtomicReference Using a custom wrapper class We can create a custom wrapper class that allows setting a value only once. The class will have a boolean flag to check if the value has already been set. Wrapper classes are used for converting primitive data types into objects. We will use this for setting ... Read More

How do we find out if first character of a string is a number in java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:40:40

4K+ Views

In this tutorial, we will learn how to find out if the first character of a String is a digit in Java. Following are the different ways to do so: Using the isDigit() method. Using the regular expressions. Using the charAt() and isDigit() methods. Using the isDigit() method The isDigit() method of the java.lang.Character class accepts a character as a parameter and determines whether it is a digit or not. If the given character is a digit, this method returns true otherwise, this method returns false. Therefore, to determine whether the first character of the given ... Read More

How to Convert a String to Hexadecimal and vice versa format in java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:37:22

18K+ Views

In this article, we will learn how to convert a string to hexadecimal and hexadecimal to string in Java. Hexadecimal is a base-16 number system that uses 16 symbols to represent a number. Those symbols can be 0-9 and A-F letters. String to Hexadecimal in Java To convert a string to hexadecimal in Java, we can use the toHexString() method of the Integer class accepts an integer as a parameter and returns a hexadecimal string. Following are the steps to convert a string to hexadecimal: Get the desired String. Create an empty StringBuffer object. Convert it into a ... Read More

How to extract an HTML tag from a String using regex in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:36:05

3K+ Views

In this article, we will see how to extract an HTML tag from a string using regex in Java. We can achieve this in multiple ways, but if we use regex, it will be better than others and also give us fast performance. What is Regex? Regex is a sequence of characters that describes a search pattern that is used to find a particular pattern in a String. It is also known as a regular expression or regexp. We use regex for pattern matching or searching or replacing a String. We will use the java.util.regex package of Java that provides ... Read More

How convert an array of Strings in a single String in java?

Maruthi Krishna
Updated on 01-Sep-2025 12:34:09

33K+ Views

In this article, we will learn how to convert an array of strings into a single string in Java. There are several ways to do so. The following are the different ways to convert an array of strings into a single string: Using the StringBuffer class. Using the toString() method of the Arrays class. Using the StringJoiner class. Using StringBuffer The StringBuffer class is used for creating mutable (modifiable) strings. It is similar to the String class, but it is mutable. The StringBuffer class is synchronized, which means it is thread-safe. To convert an array of strings into ... Read More

How can we add an underscore before each capital letter inside a java String?

Maruthi Krishna
Updated on 01-Sep-2025 12:32:35

3K+ Views

In this article, we will learn how to add an underscore before each capital letter in a Java String. We also call this process as "camel case to snake case" conversion. We can solve this problem using the following ways: Using StringBuffer class. Using Regular Expressions. Using StringBuffer class We will use the method called append() of the StringBuffer class to add an underscore. In this method, we will traverse through each character of the string and check if it is an uppercase letter. If it is, we will append ... Read More

How do we split a string with any whitespace chars as delimiters using java?

Maruthi Krishna
Updated on 01-Sep-2025 12:30:57

848 Views

What is split() method in Java? The split() method of the String class accepts a delimiter (in the form of a string), divides the current String into smaller strings based on the delimiter, and returns the resulting strings as an array. If the String does not contain the specified delimiter, this method returns an array that contains only the current string. If the String does not contain the specified delimiter, this method returns an array containing the whole string as an element. Splitting the string with white space as delimiter Following are steps to split a String into an array ... Read More

What are the rules of exception handling with respect to method overriding in java?

Aishwarya Naglot
Updated on 01-Sep-2025 12:24:13

735 Views

While a superclass method throws an exception when overriding it, you need to follow certain rules. We will be discussing these rules in this chapter. What is Exception Handling in Java? Exception handling is a mechanism to handle errors in the program. For example, if a program tries to divide a number by zero, it will throw an ArithmeticException. In such cases, the program will terminate abnormally. To avoid this, we can use exception handling. Now, let's discuss the rules of exception handling with respect to method overriding. Those are: Should throw the same exception or a subtype ... Read More

Sort an Array of Triplet using Java Comparable and Comparator

Shriansh Kumar
Updated on 01-Sep-2025 12:16:33

369 Views

In this article, we will create an array of triplet and try to sort them using the Comparable and Comparator interfaces. The term array of triplet means an array having three elements. An Array is a fixed-length linear data structure that stores group of elements with similar datatypes in a sequential manner. Sort an Array of Triplet using Comparator As the name suggests, Comparator is used to compare something. In Java, the Comparator is an interface that can be passed as a parameter to sorting methods like Arrays.sort() or Collections.sort() to sort custom objects. To use it, we need to ... Read More

Previous 1 ... 4 5 6 7 8 ... 7743 Next
Advertisements