-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path080-Compare-Two-Strings.java
More file actions
91 lines (68 loc) · 2.94 KB
/
Copy path080-Compare-Two-Strings.java
File metadata and controls
91 lines (68 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* ============================================================================
* Program Name : Compare Two Strings
* File Name : 080-Compare-Two-Strings.java
* Class Name : CompareTwoStrings
*
* Description:
* This program accepts two Strings from the user and compares them
* using equals(), equalsIgnoreCase(), and compareTo() methods.
*
* Objective:
* - Understand different ways to compare Strings in Java.
* - Learn the use of equals(), equalsIgnoreCase(), and compareTo().
* - Display comparison results.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
import java.util.Scanner;
public class CompareTwoStrings {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Create a Scanner object to read input from the keyboard.
Scanner scanner = new Scanner(System.in);
// Ask the user to enter the first string.
System.out.print("Enter the First String: ");
// Read the first string.
String firstString = scanner.nextLine();
// Ask the user to enter the second string.
System.out.print("Enter the Second String: ");
// Read the second string.
String secondString = scanner.nextLine();
// Compare the strings using equals() method.
boolean equalsResult = firstString.equals(secondString);
// Compare the strings using equalsIgnoreCase() method.
boolean ignoreCaseResult = firstString.equalsIgnoreCase(secondString);
// Compare the strings using compareTo() method.
int compareToResult = firstString.compareTo(secondString);
// Display the result of equals().
System.out.println("Using equals(): " + equalsResult);
// Display the result of equalsIgnoreCase().
System.out.println("Using equalsIgnoreCase(): " + ignoreCaseResult);
// Display the result of compareTo().
System.out.println("Using compareTo(): " + compareToResult);
// Explain the compareTo() result.
if (compareToResult == 0) {
// Both strings are equal.
System.out.println("Both Strings are Equal.");
} else if (compareToResult > 0) {
// The first string is greater.
System.out.println("First String is Greater than Second String.");
} else {
// The second string is greater.
System.out.println("First String is Less than Second String.");
}
// Example Output:
// Enter the First String: Java
// Enter the Second String: java
// Using equals(): false
// Using equalsIgnoreCase(): true
// Using compareTo(): -32
// First String is Less than Second String.
// Close the Scanner object to release system resources.
scanner.close();
}
}