Programming Articles - Page 2919 of 3363

Validate Phone with Java Regular Expressions

Krantik Chavan
Updated on 25-Jun-2020 15:01:54

396 Views

In order to match the phone using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.Declaration − The java.lang.String.matches() method is declared as follows −public boolean matches(String regex)Let us see a program to validate a phone number with regular expressions −Example Live Demopublic class Example {    public static void main( String[] args ) {       System.out.println(phone("+91 1023456789"));    }    // validate zip    public static boolean phone( String z ) {       return z.matches("\+[0-9]*\s+\d{10}" );   ... Read More

Java Program to implement Binary Search on long array

George John
Updated on 25-Jun-2020 15:03:26

302 Views

Binary search on a long array can be implemented by using the method java.util.Arrays.binarySearch(). This method returns the index of the required long element if it is available in the array, otherwise it returns (-(insertion point) - 1) where the insertion point is the position at which the element would be inserted into the array.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       long long_arr[] = { 250L, 500L, 175L, 90L, 415L };       Arrays.sort(long_arr);       System.out.print("The sorted array ... Read More

Format currency with Java MessageFormat

Ankith Reddy
Updated on 25-Jun-2020 15:07:07

607 Views

To format message with currency fillers in Java, we use the MessageFormat class. The MessageFormat class gives us a way to produce concatenated messages which are not dependent on the language. The MessageFormat class extends the Serializable and Cloneable interfaces.Declaration −The java.text.MessageFormat class is declared as follows −public class MessageFormat extends FormatThe MessageFormat.format(pattern, params) method formats the message and fills in the missing parts using the objects in the params array matching up the argument numbers and the array indices.The format method has two arguments, a pattern and an array of arguments. The pattern contains placeholder in {} curly braces ... Read More

Format floating point with Java MessageFormat

Arjun Thakur
Updated on 25-Jun-2020 15:11:20

1K+ Views

To format message with floating point fillers in Java, we use the MessageFormat class. The MessageFormat class gives us a way to produce concatenated messages which are not dependent on the language. The MessageFormat class extends the Serializable and Cloneable interfaces.Declaration −The java.text.MessageFormat class is declared as follows −public class MessageFormat extends FormatThe MessageFormat.format(pattern, params) method formats the message and fills in the missing parts using the objects in the params array matching up the argument numbers and the array indices.The format method has two arguments, a pattern and an array of arguments. The pattern contains placeholder in {} curly ... Read More

Validate the ZIP code with Java Regular expressions

Anvi Jain
Updated on 25-Jun-2020 15:04:40

499 Views

In order to match the ZIP code using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.Declaration − The java.lang.String.matches() method is declared as follows −public boolean matches(String regex)Let us see a program which validates the ZIP code with Java Regular Expressions −Example Live Demopublic class Example {    public static void main( String[] args ) {       System.out.println(zipIndia("400709"));       System.out.println(zipUS("10060"));    }    // validate zip    public static boolean zipIndia( String z ) {     ... Read More

Validate city and state with Java Regular Expressions

Smita Kapse
Updated on 25-Jun-2020 15:12:20

1K+ Views

In order to match the city and state using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression.Declaration − The java.lang.String.matches() method is declared as follows −Example Live Demopublic class Example {    public static void main( String[] args ) {       System.out.println(city("Mumbai"));       System.out.println(state("Goa"));    }    // validating the city    public static boolean city( String c ) {       return c.matches( "([a - zA - Z] + |[a - zA - Z] + ... Read More

How to catch an assertion error in Java

Nancy Den
Updated on 25-Jun-2020 15:15:15

8K+ Views

In order to catch the assertion error, we need to declare the assertion statement in the try block with the second expression being the message to be displayed and catch the assertion error in the catch block.Let us see a program on how to handle Assertion Error in Java −Examplepublic class Example {    public static void main(String[] args) throws Exception {       try {          assert args.length > 0 : "Assertion error caught!!!!";       }       catch (AssertionError e) {          System.out.println(e.getMessage());       }    } }Output

How to handle Assertion Error in Java?

Krantik Chavan
Updated on 25-Jun-2020 15:15:59

7K+ Views

In order to handle the assertion error, we need to declare the assertion statement in the try block and catch the assertion error in the catch block.Let us see a program on how to handle Assertion Error in Java −Examplepublic class Example {    public static void main(String[] args) throws Exception {       try {          assert args.length > 0;       }       catch (AssertionError e) {          System.out.println(e.getMessage());       }    } }Output

Enable Assertions from the command line in Java

Anvi Jain
Updated on 26-Jun-2020 06:46:07

522 Views

By default, assertions are disabled in Java. In order to enable them we use the following command −java -ea Example (or) java -enableassertions ExampleHere, Example is the name of the Java file.Let us see an example for generation of an assertion error by the JVM −Example Live Demopublic class Example {    public static void main(String[] args) {       int age = 14;       assert age >= 18 : "Cannot Vote";       System.out.println("The voter's age is " + age);    } }OutputThe voter's age is 14

How to compile assert in Java?

Smita Kapse
Updated on 26-Jun-2020 06:50:33

700 Views

In order to compile assert in Java, we simply set the boolean expression as false.Let us see an example program −Example Live Demopublic class Example {    public static void main(String[] args) {       assert false;       System.out.println("Compiled and executed successfully!!!");    } }OutputCompiled and executed successfully!!!

Advertisements