WHAT'S NEW?
Loading...

Comparing String method in Java


In Java programming language, comparing string method also used compareTo keyword to compared character by character using the Unicode collating sequence. This kind of methods reads the number of character and compared it to the next character, if the character matched to the compared character, next character will be read again to match, if there’s no character will the same to the next, next character will be read  and compared it. For instance, supposed string “cat”, cat is less than “dog” character, because in alphabetical order c is first character to read than to d. Another example, supposed String “met and “meat”. When you noticed the first two characters are both the same in “me” at met and “me” at meat, so, the next third character will be read and “a” is less that “t”. The evaluated out first will be “met”. How about the similar length of character and same word, like for example string “Java” and “java”. To read, string “Java” is less than the string “java” because the first character “J” of “Java” is less than the first character “j” of “java”. To make it clear, I will give a sample code in java using comparing string method in java.

Sample code 1:

String h1 = "met";
String h1 ="meat";
             
System.out.println(h1.compareTo(h2));
 if (h1.compareTo(h2)<0)
  {
   System.out.println("It is true");
  }else
   {
    System.out.println("It is false");
   }

Output:
19
It is false

Why it is become 19? When you observed, string h1 and h2 value both the same for the two characters ‘m’ and ‘e’. So, the next character ‘a’ and ‘t’ next to be compare. Now, count the alphabet from “a” to “t” start counting from “b”. The if condition will determine if it is return true or false. So, the result is false because string “met” is less than “meat”. If you are going to reverse h1 string to “meat” and h2 to “met” the equivalent output is -19 and it returns to true because -19<0.

Sample code 2:

String h1 = "java";
String h2 = "Java";
             
System.out.println(h1.compareTo(h2));
 if (h1.compareTo(h2)<0)
  {
   System.out.println("It is true");
  }else
   {
    System.out.println("It is false");
   }

Output:
32
It is false

In addition, methods equals in java, also similar in comparing a string. However, it returns the value true or false. The good thing is we can compare two strings to determine if it is equal to the given value. For example, guessing game, if the user didn’t guess what is given, will display and error message otherwise you’ve got it. Here is the sample code.

String name;
String guess = "Java";
                  
System.out.print("Guess me: ");
name = input.nextLine();
             
System.out.print("Confirm: ");
guess = input.nextLine();
             
 if (name.equals(guess)==true)
  {
   System.out.println("You've got it!");
  }
  else
   {
    System.out.println("Error");
   }

Unfortunately, we can also use char data types to determine if the input of a user is less than or greater than. Here is the sample written code below. We determine using if statement where among the two is less than based on the character inputted by the user.

char ch1;
char ch2;
      
System.out.print("Enter first character: ");
ch1 = input.next().charAt(0);
System.out.print("Enter second character: ");
ch2 = input.next().charAt(0);
             
if (ch1<ch2)
 {
  System.out.println("The first character you entered is less that the second one");
 }else
  {
   System.out.println("The second character you entered is greater that the first one");
  }

0 comments:

Post a Comment