Java Basic Problems

Java: Comparison between Vector value and String Value

If you want to compare the String value stored  in a Vector with another  String value then its for you. I have given here a better Comparison solution to compare the value in

 a Vector with another String.
Here is the code example:
import java.util.Vector;
public class MainClass {

 public MainClass() {

 Vector vec=new Vector();

 vec.add(“Jhony”);

 String name=”Jhony”;

 /*********************Better Way***************************/

  if(vec.get(0).toString().trim().equalsIgnoreCase(name))

  {

   System.out.println(“First Try Name Match”); 

  }

  else

  {

   System.out.println(“First Try Name Not Matched”); 

  }

   /*********************End of: Better Way***************************/

 /*********************Not so Better Way***************************/

  if(vec.get(0).toString()==name)

  {

  System.out.println(“Second Try Name Match”);

  }

  else

  {

  System.out.println(“Second Try Name Not Matched”);

  }

  /****************End of : Not so Better Way**********************/

  }

public static void main(String[] args) {

MainClass x=new MainClass();

  }

}
 
 
 
 

 

1 thought on “Java: Comparison between Vector value and String Value”

Leave a comment