Compare Strings by Count of Character through Java Program – In this particular article, we will be talking about the code to compare whether two user-defined strings are equal in length using Java language.
Suitable examples and sample output will be provided accordingly.
- The problem here is to check whether two given strings are equal in length.
- The input here is two user-defined strings of either the same or different lengths.
- The output here is a boolean value that helps determine whether the input strings are equal in length.
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 |
import java.util.Scanner; class CompareStrings { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String s1 = sc.nextLine(); System.out.println("Enter the string"); String s2 = sc.nextLine(); System.out.print("compare(\""+s1+"\",\""+s2+"\")--------->"+compare(s1,s2)); } static boolean compare(String s1,String s2) { if(s1.length()==s2.length()) return true; return false; } } |
Output – 1 :
- Solution:
- In the first line, a new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java.
- Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
- The next line prints a statement to the console instructing the user to enter a string.
- The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the variable s1.
- This same process is repeated again to store the second string in variable s2.
- Within, the print statement of the last line, the method compare is called and s1 and s2 are passed in as arguments.
1 2 3 4 5 |
static boolean compare(String s1,String s2) { if(s1.length()==s2.length()) return true; return false; } |
- In this method, the conditional statement checks whether the two input parameters s1 and s2 are equal in length using the .length() method which helps determine the length of a string.
- The method returns the boolean true if the condition is satisfied i.e. the strings are equal and returns the boolean false otherwise.
- This returned boolean is printed out by the print statement in the main method, thus getting the result to compare two strings character by character.
1 2 3 4 5 |
Enter the string hello Enter the string cello compare("hello","cello")--------->true |
- Here, the input strings are hello and cello. The length of each of these strings is 5
- Thus, the conditional statement in the compare method is satisfied and the boolean true is printed out that compares Strings by Count of Characters.
Output – 2:
1 2 3 4 5 |
Enter the string ddd\ Enter the string qq compare("ddd\","qq")--------->false |
- Here, the input strings are ddd\ and gg. The length of the first string is 4 and the second one is 2 and thus the conditional statement in the compare method is not satisfied and the boolean false is printed out.