Object Oriented Programming Articles

Page 364 of 589

Java Program to get day of week as string

Shriansh Kumar
Shriansh Kumar
Updated on 16-Aug-2024 3K+ Views

Some applications that work on calendars require a day name to be displayed for features like scheduling tasks, events or reminders. For this purpose, Java provides various built-in classes and methods including LocalDate, Calendar and SimpleDateFormat. In this article, we will learn how to use these classes and methods in Java programs to find the day name of a week for a given date. Using LocalDate Class In this approach, we first find current date using LocalDate class and using its built-in method named getDayOfWeek(), we create a DayOfWeek Enum which can be converted to String to display day ...

Read More

Java program to join two given lists

Arjun Thakur
Arjun Thakur
Updated on 14-Aug-2024 3K+ Views

To join two given lists the addAll() method of the java.util.ArrayList class is used to insert all of the elements in the specified collection into this list. To add contents of a list to another − Problem Statement Write a program in Java to join two given lists − Input Contents of list1 ::[Apple, Orange, Banana] Contents of list2 ::[Grapes, Mango, Strawberry] Output Contents of list1 after adding list2 to it ::[Apple, Orange, Banana, Grapes, Mango, Strawberry] Steps to join two given lists Following are the steps to join two given lists − ...

Read More

Java program to create a new list with values from existing list with Lambda Expressions

karthikeya Boyini
karthikeya Boyini
Updated on 12-Aug-2024 3K+ Views

In this article, we will demonstrate how to create a new list of employee names from an existing list of Employee objects using Lambda Expressions. We will utilize Java’s Stream API to efficiently map the employee data and collect it into a new list. Lambda Expressions: Lambda expressions simplify functional programming by working with functional interfaces, which have only one method. A lambda expression provides a way to implement this method easily. Steps Following are the steps to create a new list with values from existing list with Lambda Expressions − Import the necessary classes ...

Read More

Java program for longest increasing subsequence

AmitDiwan
AmitDiwan
Updated on 12-Aug-2024 2K+ Views

In this program, we find the length of the longest increasing subsequence (LIS) in an integer array using Java programming language. An increasing subsequence is a sequence of numbers where each number is greater than the previous one. The program uses a dynamic programming approach to compute the longest increasing subsequence efficiently. This technique involves building a solution using previously computed results. Problem Statement Write a Java program to get the length of the longest increasing subsequence − Input 10, 22, 9, 33, 21, 50, 41, 60 Output The length of the longest increasing subsequence is 5 Steps get ...

Read More

Java program to count the number of consonants in a given sentence

karthikeya Boyini
karthikeya Boyini
Updated on 08-Aug-2024 10K+ Views

In this article, we will count the number of consonants in a given sentence using Java to achieve this, we will use the Scanner class for user input and a for loop to iterate through each character in the sentence. By comparing each character with the vowels a, e, i, o, u, and ignoring spaces, we will identify consonants and increment a counter accordingly. The Scanner class is part of java.util package and is commonly used to obtain input from the user. Problem Statement Given a sentence, write a Java program to count the number of consonants. As given below ...

Read More

Pattern UNIX_LINES field in Java with Examples

Maruthi Krishna
Maruthi Krishna
Updated on 08-Aug-2024 360 Views

This flag enables Unix lines mode. In the Unix lines mode, only '' is used as a line terminator and ‘\r’ is treated as a literal character. Example 1 import java.util.regex.Matcher; import java.util.regex.Pattern; public class LTERAL_Example { public static void main(String[] args) { String input = "This is the first line\r" + "This is the second line\r" + "This is the third line\r"; //Regular expression to accept ...

Read More

Java program to check for URL in a string

karthikeya Boyini
karthikeya Boyini
Updated on 07-Aug-2024 2K+ Views

In general, to check if a given string is a valid URL(Uniform Resource Locator), we will create a method that tries to form a URL object and catches any exceptions to determine if the string is a valid URL. By using Java's URL class and exception handling, we will demonstrate a straightforward approach to verify the correctness of URL strings. We can import from java.net package object is created from the input string, then converted to a URI using toURI() method. Problem Statement A program can be created to check if a string is a correct URL or not. An ...

Read More

Java System.nanoTime() vs System.currentTimeMillis

Rudradev Das
Rudradev Das
Updated on 07-Aug-2024 2K+ Views

What are the Time Operations in Java? There are two-time operations provided by the Java environment. For time-related operations, users can use these operations. System.nanoTime() System.currentTimeMillis() System.nanoTime() mainly known as an expensive call, is used to get a more specific value of time. And, System.currentTimeMillis() most authentic possible passed time, helps to get the time value based on the operating system. ...

Read More

How to Read a File into an ArrayList in Java?

Mr. Satyabrata
Mr. Satyabrata
Updated on 06-Aug-2024 4K+ Views

In Java, an ArrayList is a resizable array, which is implemented as part of the Java Collections Framework. It is a dynamic data structure that can hold any type of objects, including primitive types such as integers and characters. As per the problem statement we have to read a file into an ArrayList. First and foremost we will take the path of the file as input and then we will apply different method to read the file. Let's start!Example For instance Suppose the input file is “myfile.txt”. After reading the content of the file, the result ...

Read More

Java Program to Segregate 0s on Left Side & 1s on Right Side of the Array

Rudradev Das
Rudradev Das
Updated on 06-Aug-2024 2K+ Views

Segregation is a process in the field of software engineering where a code is forced to depend on those methods that are not in use. The segregation interface is known as ISP. It splits the interfaces that are vast in nature. In a Java environment, there are lots of advantages to implementing the segregation principle. It increases the readability of the particular code. It also helps to maintain that particular code conveniently. Problem Statement There is an array of 7s and 16s aimlessly. We must segregate the 7s on the left side and the 16s on the right. The basic ...

Read More
Showing 3631–3640 of 5,881 articles
« Prev 1 362 363 364 365 366 589 Next »
Advertisements