Found 9150 Articles for Object Oriented Programming

Java program to access character of a string

karthikeya Boyini
Updated on 11-Nov-2024 19:16:55

35K+ Views

In this article, we will access a specific character from a string by using the charAt() method in Java. The program will demonstrate how to locate and display a character at a specified position within a string. Problem Statement We have a string, and we need to retrieve the character at a given position. For example, if we use the string "laptop", we want to display the character located at the 4th position (0-based index). Input "laptop" Output String: laptop Character at 4th position: t Steps to access character of a string The following an steps to access the ... Read More

Java program to reverse an array upto a given position

Samual Sam
Updated on 15-Oct-2024 11:54:38

874 Views

In this article, we will reverse the elements of an array up to a specified position using Java. You will learn how the program takes an array, reverses the first part of it up to a given index, and prints the modified array. An array can be reversed upto the given position pos and the remaining array is unchanged.Problem Statement Write a Java program to reverse an array upto a given position.Input Array = 1 2 3 4 5 6 7 8 9 10 Position = 6 Output Modified array is: 6 5 4 3 2 1 7 8 9 ... Read More

Java program to check for URL in a string

karthikeya Boyini
Updated on 07-Aug-2024 21:46:44

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 program to check whether a given string is Heterogram or not

Alshifa Hasnain
Updated on 17-Dec-2024 03:31:34

506 Views

A heterogram is a string where each letter appears only once, with no repeated characters. This property makes it unique and an interesting problem to solve in Java. In this article, we will learn to determine whether a given string is a heterogram using a frequency-based approach and introduce an alternative using a Set. Set A Set is a type of Collection designed to hold unique elements, ensuring no duplicates are present. It reflects the concept of a mathematical set and includes methods from the Collection interface while enforcing this rule of uniqueness. Using an Array for Character Frequency  This ... Read More

Check if a String is not empty ("") and not null in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:03:23

2K+ Views

Let’s say we have the following string −String myStr1 = "Jack Sparrow";Let us check the string now whether it is not null or not empty.if(myStr != null || myStr.length() != 0) { System.out.println("String is not null or not empty");Example Live Demopublic class Demo {    public static void main(String[] args) {       String myStr = "Jack Sparrow";       boolean res;       if(myStr != null || myStr.length() != 0) {          System.out.println("String is not null or not empty");       } else {          System.out.println("String is null or empty");       }    } }OutputString is not null or not empty

Java program to reverse each word in a sentence

Shriansh Kumar
Updated on 25-Sep-2024 18:27:47

2K+ Views

Given a sentence or string, write a Java program to reverse each word in that sentence. Each word in that sentence should be reversed and at the same time, they should be in the same order as before. Let's understand the problem with an example − Example Scenario: Input: orgnl_sen = an apple is red Output: modfd_sen = na elppa si der Using Iteration First, split the given sentence into an array of words using the space character as the delimiter. Use a for-each loop to iterate over each word. Then, iterate through the characters of each ... Read More

Add seconds to current date using Calendar.add() method in Java

karthikeya Boyini
Updated on 25-Jun-2020 13:11:53

2K+ Views

Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us increment the seconds using the calendar.add() method and Calendar.SECOND constant.calendar.add(Calendar.SECOND, 15);Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Add 15 seconds to current date       calendar.add(Calendar.SECOND, 15);       System.out.println("Updated Date = " + calendar.getTime());    } ... Read More

Java Program to subtract months from current date using Calendar.add() method

Samual Sam
Updated on 08-Jul-2024 18:03:44

3K+ Views

When working with dates in Java, it's common to need to increment or decrement months. The Calendar class makes it easy to manipulate dates. This article shows how to decrement the current date by a specified number of months using the Calendar class. Problem Statement Given a Java program to decrement the current date by a specified number of months using the Calendar class. Output Current Date = Thu Nov 22 16:37:42 UTC 2018 Updated Date = Thu Mar 22 16:37:42 UTC 2018 Basic Approach Below are the steps to subtract months from current date using Calendar.add() ... Read More

Add months to current date using Calendar.add() method in Java

Aishwarya Naglot
Updated on 21-Jul-2025 11:31:51

5K+ Views

The given task is to add months to a date (object) using the add() method of the Calendar class. For example, if we need to add 5 months to the date: 26-06-2025, the new date would be 26-11-2025. Calendar.add() method in Java The calender.add() method in Java is used to add a specified amount of time to any field of the Calendar. Using this method, we can add months, years, days, etc. This method accepts two parameters: the first one is the field we want to add to, and the second one is the amount we want to ... Read More

Subtract minutes from current time using Calendar.add() method in Java

Samual Sam
Updated on 25-Jun-2020 12:16:40

2K+ Views

Import the following package for Calendar class in Java.import java.util.Calendar;Firstly, create a Calendar object and display the current date and time.Calendar calendar = Calendar.getInstance(); System.out.println("Current Date and Time = " + calendar.getTime());Now, let us decrement the minutes using the calendar.add() method and Calendar.MINUTE constant. Set a negative value since you want to decrease the minutes.calendar.add(Calendar.MINUTE, -15);Example Live Demoimport java.util.Calendar; public class Demo {    public static void main(String[] args) {       Calendar calendar = Calendar.getInstance();       System.out.println("Current Date = " + calendar.getTime());       // Subtract 15 minutes from current date       calendar.add(Calendar.MINUTE, -15); ... Read More

Advertisements