Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Program to Compare two Strings!

In this post, we are going to see how to compare two strings using Java. There are multiple ways we can compare strings in Java. I am going to show you the most commonly used ways here.

1. First method ( using equals() ) – Recommended: 

/**
 *
 * @author agurchand
 */
public class CompareTwoStrings {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String str1 = "agur";
        String str2 = "agur";
        
        if ( str1.equals(str2) ) {
            System.out.println("Both are equal!");
        } else {
            System.out.println("Both are not equal!");
        }
    }
    
}

The output of the above program will be:

Both are equal!

BUILD SUCCESSFUL in 302ms

2. Second method ( using == ): 

/**
 *
 * @author agurchand
 */
public class CompareTwoStrings {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String str1 = "agur";
        String str2 = "babu";
        
        if ( str1 == str2 ) {
            System.out.println("Both are equal!");
        } else {
            System.out.println("Both are not equal!");
        }
    }
    
}

The output of the above program will be:

Both are not equal!

BUILD SUCCESSFUL in 302ms

Let’s write another example Java program to get the strings from the user and then compare the strings. 

We are going to use the Java Scanner to get the input from the user, so let’s see how we can do it.

import java.util.Scanner;

/**
 *
 * @author agurchand
 */
public class CompareTwoStrings {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String str1, str2;
        
         //create a scanner object to get the input
        Scanner in = new Scanner(System.in);
        
        //get the str1 value from user
        System.out.print("Enter the first string:");
        str1 = in.next();

        //get the str2 value from user
        System.out.print("Enter the second string:");
        str2 = in.next();

        //compare both using equals()
        if ( str1.equals(str2) ) {
            System.out.println("Both the strings are equal!");
        } else {
            System.out.println("Both the strings are not equal!");
        }
    }
    
}

Read the inline comments in the above Java Program to understand each and every line.

Here is another example using BufferReader:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author agurchand
 */
public class CompareTwoStrings {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {

        String str1, str2;        
        
        //create a scanner object to get the input
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        
        //get the str1 value from user
        System.out.print("Enter the first string:");
        str1 = in.readLine();

        //get the str2 value from user
        System.out.print("Enter the second string:");
        str2 = in.readLine();

        //compare both using equals()
        if ( str1.equals(str2) ) {
            System.out.println("Both the strings are equal!");
        } else {
            System.out.println("Both the strings not equal!");
        }
    }
    
}

Enjoy the day!

The post Java Program to Compare two Strings! appeared first on TutorialsMade.



This post first appeared on TutorialsMade - Ultimate Tutorial, please read the originial post: here

Share the post

Java Program to Compare two Strings!

×

Subscribe to Tutorialsmade - Ultimate Tutorial

Get updates delivered right to your inbox!

Thank you for your subscription

×