Found 33676 Articles for Programming

Java program to divide a string in \\'N\\' equal parts

Alshifa Hasnain
Updated on 23-Oct-2024 17:32:12

839 Views

In this article, we will understand how to divide a string into 'N' equal parts using Java. If the string's length is divisible by N, the string will be split evenly into N parts; otherwise, a message will be displayed indicating that the string cannot be divided into N equal parts. This is demonstrated using both a simple main method and an encapsulated approach. Problem Statement Write a program in Java to divide a string into 'N' equal parts. Below is a demonstration of the same − Input Input string: Java Program is fun! Output The length of the string ... Read More

Java program to get minimum and maximum from a list

Aishwarya Naglot
Updated on 24-Jun-2025 12:51:19

1K+ Views

Minimum and Maximum Values of a ListA list is an ordered collection that allows us to store and access elements sequentially. It contains index-based methods to insert, update, delete, and search the elements. It can also have duplicate elements. The following are the ways to get the minimum and maximum from a list in Java: Using Loops Using Collections Using Streams Minimum and Maximum of a List Using LoopsWe can find the minimum and maximum values in a list by iterating through the elements using loops. We will initialize two variables to save the minimum and maximum values, and ... Read More

Java program to check if a string is empty or null

AmitDiwan
Updated on 08-Nov-2024 22:31:26

993 Views

In this article, we will understand how to check if a string is empty or null in Java. The string is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). We’ll start by learning to detect these cases directly within the main method, and then we’ll see how to achieve the same result using encapsulation for better code organization and reusability. Problem Statement Write a program in Java to check if a string is empty or null. Below is a demonstration of the same − Input Input string: null Output The string is a null ... Read More

Java program to remove all whitespaces from a string

AmitDiwan
Updated on 07-Nov-2024 01:05:37

395 Views

In this article, we will understand how to remove all whitespaces from a string in Java. The String class in Java represents a sequence of characters enclosed in double-quotes. Removing whitespaces from a string is a common operation, especially when processing user input or cleaning data. The replaceAll() method is used with a regular expression to match whitespace characters and replace them with an empty string. Problem StatementGiven a string with whitespaces, write a Java program to remove all whitespaces from the string.Input Initial String = "Java programming is fun to learn."Output String after removing white spaces = ... Read More

Java program to convert array into collection

AmitDiwan
Updated on 04-Nov-2024 18:44:02

796 Views

In this article, we will understand how to convert the array into a collection in Java. The Collection is a framework that provides an architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on data such as searching, sorting, insertion, manipulation, and deletion. Problem Statement Write a program in Java to convert the array into the collection. Below is a demonstration of the same − Input Input array: [Java, Python, Scala, Shell] Output After elements after converting the array to a list ... Read More

Java program to convert collection into array

Aishwarya Naglot
Updated on 24-Jun-2025 13:23:12

1K+ Views

The Collection is a framework that provides architecture to store and manipulate a group of objects. Java Collections can achieve all the operations that you perform on data, such as searching, sorting, insertion, manipulation, and deletion. In this article, we will understand how to convert a collection into an array in Java. Following are the ways to convert a collection into an array in Java: Using Iteration Using toArray() Method Using Streams Collection to Array using Iteration The naive way to convert a collection into an array is by traversing the collection and adding each element in the ... Read More

Java Program to Check if a string contains a substring

Aishwarya Naglot
Updated on 04-Aug-2025 18:51:48

3K+ Views

A string is a class in Java that stores a series of characters enclosed within double quotes, and a continuous sequence of characters within that string is known as a substring. Those characters are string-type objects. In this article, we will learn how to write Java programs to check if a string contains a substring or not.  Java Program to Check if a String Contains a Substring We can check whether the given string contains a substring or not in the following ways: Using indexOf() Method Using contains() Method ... Read More

Java Program to Shuffle the Elements of a Collection

Aishwarya Naglot
Updated on 24-Jun-2025 12:37:37

393 Views

The Collection is a framework that provides an architecture to store and manipulate a group of objects. In Java, Collections can perform all the operations that you perform on data, such as searching, sorting, insertion, manipulation, and deletion. In this article, we will learn how to shuffle the elements of a collection in Java. Shuffling refers to randomly rearranging the elements of a collection. Let's take an example as follows: Input: list: [Java, program, is, fun, and, easy] Output: list: [fun, easy, Java, program, and, is] Ways to Shuffle Elements of a Collection in Java ... Read More

Java Program to Format time in AM-PM format

Shriansh Kumar
Updated on 01-Aug-2024 10:59:17

2K+ Views

Time formatting is a process of converting date and time values into human-readable strings with specific formats. The resulting string describes how date/time values should be represented (such as flat files or human-readable output). In this article, we will understand how to format time in AM-PM format. In 12-hour clock system, AM-PM format is used to define time. Here, AM stands for Ante meridiem which refers to the time before noon, whereas PM stands for Post meridiem which shows the time period after noon. Example Scenario Input: current_date = Thu Mar 17 16:04:31 IST 2024 Output: Current Time ... Read More

Java Program to Get the Size of the Collection

Aishwarya Naglot
Updated on 21-Aug-2025 12:32:32

209 Views

The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion. In this article we will look at how to get the size of a collection in Java. The size of a collection is the number of elements it contains. Following are the ways to get the size of a collection in Java: Using size() Method Using loops Using Streams API Getting Size of Collection Using size() Method The size() method is ... Read More

Advertisements