Found 33676 Articles for Programming

Java Program to Match Zip Codes

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

408 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

195 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

135 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

189 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

265 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

211 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

Ternary Operator in Java

Samual Sam
Updated on 25-Jun-2020 11:14:54

2K+ Views

A ternary operator uses 3 operands and it can be used to replace the if else statement. This can be done to make the code simpler and more compact.The syntax of the ternary operator is given as follows −Expression ? Statement 1 : Statement 2In the above syntax, the expression is a conditional expression that results in true or false. If the value of the expression is true, then statement 1 is executed otherwise statement 2 is executed.A program that demonstrates the ternary operator in Java is given as follows.Example Live Demopublic class Example {    public static void main(String[] args) ... Read More

Java program to print the diamond shape

Samual Sam
Updated on 23-Nov-2024 03:55:38

338 Views

In this article, we will learn how to print a diamond shape pattern using nested loops in Java. This helps in understanding loops and conditional structures in Java programming.A nested loop refers to a loop placed within another loop. This structure is often called "loops within loops" because the inner loop runs completely each time the outer loop iterates.Diamond Shape A diamond shape is a symmetrical pattern that consists of two parts: an upper triangle and a mirrored inverted triangle below it. In programming, such patterns are typically printed using nested loops to control spaces and symbols. A diamond ... Read More

Basic calculator program using Java

karthikeya Boyini
Updated on 19-Aug-2024 18:30:26

24K+ Views

In this article, we will learn to create a basic calculator using Java. With a basic calculator, we can add, subtract, multiply, or divide two numbers. This is done using a switch case. A program that demonstrates this is given as follows − Problem Statement Write a program in Java to create a basic calculator for performing the basic arithmetic operations − Input Enter two numbers: 23Enter an operator (+, -, *, /): + Output The result is given as follows:2.0 + 3.0 = 5.0 Steps to create basic calculator Following are the steps to create a basic calculator program using ... Read More

Advertisements