Java Articles

Page 124 of 450

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

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 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
Aishwarya Naglot
Updated on 01-Sep-2025 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 to Convert a String to Hexadecimal and vice versa format in java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 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
Aishwarya Naglot
Updated on 01-Sep-2025 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
Maruthi Krishna
Updated on 01-Sep-2025 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
Maruthi Krishna
Updated on 01-Sep-2025 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

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

Aishwarya Naglot
Aishwarya Naglot
Updated on 01-Sep-2025 856 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
Shriansh Kumar
Updated on 01-Sep-2025 498 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

Simple Calculator using TCP in Java

Shriansh Kumar
Shriansh Kumar
Updated on 01-Sep-2025 1K+ Views

The Internet Protocol suite contains all sort of protocols that enables communication between devices over the Internet. TCP is a connection-oriented protocol, which means it maintains the established connection between two devices till the end of a communication. This is the reason it is used while web surfing, sending emails and transferring files. In this article, we will develop a simple client-server side calculator using TCP in Java. The client will request the operation and the server will send the result to client device after calculating it. Java Networking Let's briefly understand a few basic concepts about Java networking that ...

Read More

Shuffling Elements of Unordered Collections in Java

Shriansh Kumar
Shriansh Kumar
Updated on 01-Sep-2025 1K+ Views

There are two types of Collections in Java. One is ordered and the other one is unordered collection. The ordered collections store their elements in the order in which they are inserted i.e. it maintains the insertion order of elements. Whereas, the unordered collections such as Map and Set do not maintain any order. In this article, we will create an unordered collection and try to shuffle its elements using the inbuilt method Collections.shuffle(). The Collections.shuffle() Method The Collections.shuffle() method is provided by java.util package. It takes a list as an argument and then rearranges the elements randomly. Therefore, before ...

Read More
Showing 1231–1240 of 4,498 articles
« Prev 1 122 123 124 125 126 450 Next »
Advertisements