MessageFormat equals() method in Java

This tutorial will help you to learn how to implement the MessageFormat equals() method in Java Programming. MessageFormat is a package present in java.text.Format is a sub package of java.lang.Object.

The following is a syntax for the MessageFormat equals() method.

boolean equals(Object obj)

The following statement is used to check the equality of the two MessageFormats. It returns the result as a boolean value i.e, true or false. Its Modifier and type are boolean.

Implementing MessageFormat equals() method in Java

The following code gives a complete description of equals().

import java.io.*;
import java.util.*;
public class test
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter i & j values");
        int i=sc.nextInt();
        int j=sc.nextInt();
        int y=i*j;
        String text1 = java.text.MessageFormat.format("{0} X {1} = {2}",i,j,y); //Message Format1
        System.out.println("Enter k & l values");
        int k=sc.nextInt();
        int l=sc.nextInt();
        int z=i*j;
        String text2 = java.text.MessageFormat.format("{0} X {1} = {2}",k,l,z);  //Message Format2
        if(text1.equals(text2))                    //Comparison by using equals()
        {
            System.out.println("text1 equals text2");
        }
        else
        {
            System.out.println("text1 does not equal text2");
        }
        
}
}

Test Case 1:

4
5
4
5
text1 equals text2

Test Case 2:

4
5
5
5
text1 does not equal text2

Explanation:

Here we used MessageFormats for reading text1 and text2 and then by using equals() we compared both text1 and text2 if they are equal it returns true else it returns false.

Reference:

https://docs.oracle.com/javase/7/docs/api/java/text/MessageFormat.html#equals(java.lang.Object)

Also read: URL Encoding and Decoding using Java

Leave a Reply

Your email address will not be published. Required fields are marked *