3 Replies - 760 Views - Last Post: 28 October 2007 - 01:47 PM Rate Topic: -----

#1 kensington  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-October 07

Checking if values are the same

Posted 28 October 2007 - 11:14 AM

I currently have two String objects I check to find out if they are the same value:
String str1 = "red";
String str2 = "yellow";
if (str1.equals(str2)){
	  System.out.println("Equal");
}
else{
	  System.out.println("Not equal");
}

Now I need to check 10 objects. How would I check 10 objects to find out if any of them have the same value?

Please advise.

Is This A Good Question/Topic? 0
  • +

Replies To: Checking if values are the same

#2 brawnyman713  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 139
  • Joined: 21-October 07

Re: Checking if values are the same

Posted 28 October 2007 - 11:36 AM

What do you mean by objects? Do you want to check if some value in all of the objects is the same? If you want some value to be the same for all objects, just set the variable to static. If thats not what you want, then tell me what part of the object you wanna check
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Checking if values are the same

Posted 28 October 2007 - 11:38 AM

Well there are two main ways of checking for equality on objects. One is checking to see if the object refers to the exact same object... that is the references point at the same memory address. This is done using the operator "==" as I am sure you are aware of.

If you look up the equals() method you will only find it up on the superclass "object". This method essentially does the same thing as "==" and compares the references.

Now normally you would do such a comparison for custom types by overloading the == operator in languages like C++ or C# but Java has yet to support this. What you can do though is write a method in your custom object which takes another custom object as a parameter and then outline the comparison... like comparing values of members etc and if the tests pass, they are equal.

If you are talking about standard objects like labels etc, you can use the == or compare their text by using getText and do an equals on that like is done below...

if (label1.getText().equals(label2.getText())) {
	   System.out.println("The labels are equal text.");
}



I hope this is making some sense. So in short, you can use == to compare references pointing at the same object, you can use equals() on object to do the same thing as ==, you can write a custom function on your custom object to make your own comparison, or you can pull out data and use its equals to compare the data pulled out of another object.

Hope this helps! :)

This post has been edited by Martyr2: 28 October 2007 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 kensington  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 08-October 07

Re: Checking if values are the same

Posted 28 October 2007 - 01:47 PM

Thanks for all the info.

I basically want to check if any of the 10 variables have duplicate values:
String str1 = "red";
String str2 = "yellow";
String str3 = "green";
String str4 = "blue";
String str5 = "red";
String str6 = "green";
String str7 = "green";
String str8 = "white";
String str9 = "black";
String str10 = "green";


In this example red would have 2 duplicates, green would have 4 duplicates.

Please advise.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1