View RSS Feed

Java String

Compare strings

Rate this Entry
by , 04-26-2012 at 06:58 PM (436 Views)
Two strings comparison will be learnt in this section. Java lang package gives methods for comparison of 2 with either lower or upper case. Facility is being provided by the equals() method to compare 2 strings. Equal() methods are used by the given program to help you for comparing 2 strings. In case when both of the strings present are equal then a message “Given strings equal” would be displayed. Otherwise, “given strings unequal” would be shown.
Description of code:
equals():

This method is used for comparison of the object values. Also, Boolean type values are returned either as false or true. If true is returned for objects then it means it is equal, otherwise not.
Code is given below for this program:
Java Code:
import java.lang.*;
import java.io.*;

public class CompString{
  public static void main(String[] args) throws IOException{
  System.out.println("String equals or not example!");
  BufferedReader bf = 
new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Please enter first string:");
  String str1 = bf.readLine();
  System.out.println("Please enter second string:");
  String str2 = bf.readLine();
  if (str1.equals(str2)){
  System.out.println("The given string is equals");
  }
  else{
  System.out.println("The given string is not equals");
  }
  }
}

Submit "Compare strings" to Facebook Submit "Compare strings" to Digg Submit "Compare strings" to del.icio.us Submit "Compare strings" to StumbleUpon Submit "Compare strings" to Google

Categories
Compare

Comments