How to complete 2 string in Java program

import java.util.Scanner;
class CompareStrings {
  public static void main(String args[]) {
    String s1, s2;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the first string");
    s1 = in.nextLine();
    System.out.println("Enter the second string");
    s2 = in.nextLine();
    if (s1.compareTo(s2) > 0)
      System.out.println("First string is greater than second.");
    else if (s1.compareTo(s2) < 0)
      System.out.println("First string is smaller than second.");
    else
      System.out.println("Both strings are equal.");
  }
}

Step-by-Step Explanation:

1. Import Scanner Class:

import java.util.Scanner;

The Scanner class from java.util is imported so that it can read what the user inputs.

2.  Class Declaration:

class CompareStrings {

A class called CompareStrings is declared, consisting of the logic to compare two strings.

3.  Main Method Declaration:

public static void main(String args[]) {

The main method is the starting point of a program, and execution begins there.

4. String Declaration:

String s1, s2;

Above, two string variables are declared to store the string entered by a user in their respective variables.

5. Scanner Declaration:

Scanner in = new Scanner(System.in);

A variable of type Scanner called in for getting the console input.

6.  Ask for and Read the First String:

System.out.println(“Enter the first string”);

s1 = in.nextLine();

This program asks the user to provide the first string and reads into the variable s1.

7.  Ask for and Read the Second String:

System.out.println(“Enter the second string”);

s2 = in.nextLine();

The program will ask the user to input the second string, and read that into the variable s2.

8. Compare the Strings:

if (s1.compareTo(s2) > 0) 

{

      System.out.println(“First string is greater than second.”);

else if (s1.compareTo(s2) < 0) 

{

System.out.println(“First string is smaller than second.”);

 }

else 

{

     System.out.println(“Both strings are equal.”);

 }

The program uses the compareTo() method to compare the two strings: Comparing Strings using compareTo() Method

The compareTo() method compares two strings lexicographically, or according to Unicode value of characters:

  • If s1.compareTo(s2) return a value greater than 0 then its because s1 is lexicographically greater than s2.
  • If s1.compareTo(s2) returns a value less than 0, it means that s1 is lexicographically smaller than s2.
  • When s1.compareTo(s2) returns 0 then it compares both the strings are equal.

9. Print Comparison Value:

Depending on the result from the comparison method of compareTo() the program is going to print one of these two.

  • “First string is greater than second.”
  • “First string is smaller than second.”
  • “Both strings are equal.”

Example Runs:

#Example 1: Strings are Different and the First String is Smaller

Input:

Enter the first string

apple

Enter the second string

banana

Run:

  • The program compares the strings  “apple” and  “banana.
  • The Unicode value of  “a” (97) is less than the Unicode value of  “b” (98), so  “apple” is lexicographically smaller than  “banana”.

#Example 2: Strings are Different and the First String is Smaller

Input:

Enter the first string

grape

Enter the second string

kiwi

Execution:

  • The program compares  “grape” and  “kiwi”.
  • The Unicode value of  “g” (103) is smaller than the Unicode value of  “k” (107), so  “grape” is lexicographically smaller than  “kiwi”.

Output:

First string is smaller than second.

#Example 3: Strings are Equal

Input:

Enter the first string

apple

Enter the second string

apple

Execution:

  • The program compares  “apple” and  “apple”.
  • Both the strings are equal. So, the compareTo() method returns 0.

Output:

Both strings are equal.

Comparing two strings lexicographically using the compareTo() method of the String class:

Printing  “First string is greater than second.” if the first string is lexicographically greater than the second.

Printing  “First string is smaller than second. ” if the first string is lexicographically smaller than the second

Printing  “Both strings are equal. ” if both strings are equal

Conclusion:

This is a very simple Java Program to demonstrate using the compareTo() method for string comparison, useful for checking which string has come first either alphabetically, lexicographically or maybe more complexly comparison.

Leave a Comment

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