Found 7442 Articles for Java

Java program to find maximum of three numbers

karthikeya Boyini
Updated on 04-Nov-2024 18:42:13

30K+ Views

In this article, we will learn how to find the maximum among three numbers using an if-else statement in Java. The if-else construct allows us to evaluate conditions and execute different blocks of code based on whether the conditions are true or false. We will check the values of three numbers to determine the largest by using comparison operators and control flow statements. Problem StatementGiven three integer values, write a Java program to find the maximum among them using if-else statements. Input num1 = 15, num2 = -5, num3 = 7 Output 15 is the maximum number. Steps to ... Read More

Java Program to Match Dates

karthikeya Boyini
Updated on 25-Jun-2020 11:49:33

182 Views

Firstly, we have considered the following two dates.SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd"); Date d1 = s.parse("2018-10-15"); Date d2 = s.parse("2018-11-10");Now, use the compareTo() method to compare both the dates. The results are displayed on the basis of the return value.if (d1.compareTo(d2) > 0) {    System.out.println("Date1 is after Date2!");    } else if (d1.compareTo(d2) < 0) {       System.out.println("Date1 is before Date2!");    } else if (d1.compareTo(d2) == 0) {       System.out.println("Date1 is equal to Date2!");    } else {       System.out.println("How to get here?"); }Example Live Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class ... Read More

Java Program to Match Zip Codes

Samual Sam
Updated on 23-Nov-2024 03:49:21

404 Views

In this article, we will learn how to validate U.S. zip codes using a regular expression in Java. The program checks if a given string is a valid U.S. zip code, either in the standard five-digit format or the extended nine-digit format.Zip Code Format In the U.S., zip codes are five digits, with each digit representing a specific part of the United States.Let’s say we have the following zip code. String zipStr = "12345"; Now, set the following regular expression to match zip codes in America. String reg = "^[0-9]{5}(?:-[0-9]{4})?$"; Matching (Validating) a ZIP CodeThe following are the steps to ... Read More

Java program to remove the leading and trailing quotes from a string

karthikeya Boyini
Updated on 19-Sep-2024 22:00:16

2K+ Views

In Java, handling strings with quotes can be managed by checking and manipulating the string’s start and end. This example demonstrates how to remove double quotes from both the beginning and end of a string. Problem Statement Given a string enclosed in double quotes, write a Java program to remove these enclosing quotes. The result must be the original string without the enclosing double quotes. Input String with double quotes= "Demo Text" Output String after removing double quotes = Demo Text Steps to remove the leading and trailing quotes from a string Below are the steps to remove the leading ... Read More

Shift left in a BigInteger in Java

Samual Sam
Updated on 25-Jun-2020 10:59:45

191 Views

To shift left in a BigInteger, use the shiftLeft() method.The java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this

Java Program to shift bits in a BigInteger

karthikeya Boyini
Updated on 25-Jun-2020 11:01:12

130 Views

To shift bits in a BigInteger, use the shiftLeft() or shiftRight() method.shiftLeft() methodThe java.math.BigInteger.shiftLeft(int n) returns a BigInteger whose value is (this > n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. It computes floor(this / 2n).Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       one = new BigInteger("25");       one = one.shiftRight(3);       System.out.println("Result: " +one);    } }OutputResult: 3

Java Program to flip a bit in a BigInteger

Samual Sam
Updated on 25-Jun-2020 11:02:24

184 Views

To flip a bit in a BigInteger in Java, use the flipBit() method. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit flipped.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       one = one.flipBit(3);       System.out.println("Result: " +one);    } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = ... Read More

Clear a bit in a BigInteger in Java

karthikeya Boyini
Updated on 25-Jun-2020 11:06:01

262 Views

To clear a bit in a BigInteger in Java, use the clearBit() method. It returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared.Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       two = one.clearBit(2);       System.out.println("Result: " +two);    } }OutputResult: 3Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new ... Read More

Set a bit for BigInteger in Java

Samual Sam
Updated on 25-Jun-2020 11:07:41

210 Views

The setBit() method is used in Java to return a BigInteger whose value is equivalent to this BigInteger with the designated bit set.Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("7");       two = one.setBit(3);       System.out.println("Result: " +two);    } }OutputResult: 15Let us see another example.Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger bi1, bi2;       bi1 = new BigInteger("9");       // setbit ... Read More

Java program to check if string is pangram

Shriansh Kumar
Updated on 18-Apr-2025 18:49:33

12K+ Views

The given task is to write a Java program to check whether a string is a pangram or not. A string is called a pangram if and only if it contains all the letters of the English alphabet, regardless of their case. Example Scenario:Let's understand the problem with an example - Input: "The quick brown fox jumps over the lazy dog" Output: Yes, the string is a pangram Read the input String, you can find all the letters of the English alphabet in it. Therefore, it is a pangram string. How to Check if a String is a Pangram ... Read More

Advertisements