Java Articles

Page 199 of 450

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 swap pair of characters

AmitDiwan
AmitDiwan
Updated on 09-Aug-2024 3K+ Views

In this article, we’ll learn how to swap pairs of characters in a Java string. We start with a simple method that converts the string into a character array, allowing us to swap characters directly. Then, we’ll explore an object-oriented approach where the swapping logic is encapsulated in a separate method. Both approaches highlight key Java concepts such as string manipulation and method encapsulation. Problem Statement Write a Java program to swap a pair of characters. Below is a demonstration of the same − Input  Input string: Java program Output The string after swapping is: Javg proaram Steps for basic ...

Read More

Java menu driven program to check positive negative or odd even number

Mr. Satyabrata
Mr. Satyabrata
Updated on 09-Aug-2024 2K+ Views

A number is said to be a positive number if it is greater than 0 and if it is less than 0 then it is called a negative number. The negative number is followed by a minus sign (-) to identify it as a negative number. A number is said to be an even number if it is divisible by 2 else it is called an odd number. In this article, we will see how to check if a number is positive, negative, or even, odd by using Java programming language. We will be implementing the application using a switch ...

Read More

Java program to print even length words

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

In this article, we will understand how to print even-length words. The string is a datatype that contains one or more characters and is enclosed in double quotes (“ ”). Char is a datatype that contains an alphabets an integer or a special character. Problem Statement Write a Java program to print even-length words. Below is a demonstration of the same − Input Input string: Java Programming are cool Output  The words with even lengths are: Java cool Approaches to print even length words Below are the different approaches to print even length words − Using ...

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

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 363 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
Showing 1981–1990 of 4,498 articles
« Prev 1 197 198 199 200 201 450 Next »
Advertisements