Home arrow static arrow Java Programming [Archive] - compare three strings
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - compare three strings
This topic has 30 replies on 3 pages.    1 | 2 | 3 | Next »

Posts:56
Registered: 9/12/99
compare three strings  
Aug 10, 2004 5:50 AM



 

Folks:

I'd like to know if three strings are equal. I can surely do

if ( (s1.equals(s2) )&& ( s2.equals(s3) )

Is there a better way? What if I have four strings?

Thanks
 

Posts:23
Registered: 7/26/02
Re: compare three strings  
Aug 10, 2004 6:37 AM (reply 1 of 30)



 
Function to handle what you want with an unlimited number of strings (assuming you put them in an array):

private boolean stringsEqual(String[] strings){   for (int i = 1; i < strings.length(); i++)   {      if (!strings[i].equals(strings[0]))         return false;   }    return true;}
 

Posts:24,036
Registered: 2/3/03
Re: compare three strings  
Aug 10, 2004 6:42 AM (reply 2 of 30)



 
That "function" is borked.
 

Posts:24,036
Registered: 2/3/03
Re: compare three strings  
Aug 10, 2004 6:43 AM (reply 3 of 30)



 
That "function" is borked.

First, it won't compile. Second, even if it did, you're only comparing the first element of the array.
 

Posts:10,972
Registered: 10/23/03
Re: compare three strings  
Aug 10, 2004 6:45 AM (reply 4 of 30)



 
And what about the case when the array has length 0, or a String reference is null?
 

Posts:24,036
Registered: 2/3/03
Re: compare three strings  
Aug 10, 2004 6:46 AM (reply 5 of 30)



 
You could add the elements to a Set and compare the size of the Set to the number of elements (Sets don't allow duplicates). Example:
String[] s = {"one", "two", "two"};List list = Arrays.asList(s);Set set = new HashSet(list);System.out.println(list.size() == set.size()); // false
 

Posts:10,972
Registered: 10/23/03
Re: compare three strings  
Aug 10, 2004 6:50 AM (reply 6 of 30)



 
public static boolean allEqual(Object[] objects) {    if (objects == null)        throw new NullPointerException();    if (objects.length == 0)        return true; //all are vacuously equal    Object first = objects[0];    if (first == null) {        for(int i=1; i<objects.length; ++i) {            if (objects[i] != null)                return false;        }    } else {        for(int i=1; i<objects.length; ++i) {            if (!first.equals(objects[i]))                return false;        }    }    return true;}
 

Posts:24,036
Registered: 2/3/03
Re: compare three strings  
Aug 10, 2004 6:51 AM (reply 7 of 30)



 
I like my way better. :oP

J/K
 

Posts:23
Registered: 7/26/02
Re: compare three strings  
Aug 10, 2004 6:55 AM (reply 8 of 30)



 
Okay, so I made one mistake that generated a compilation error, change strings.length() to strings.length.

Second, yes I'm comparing the first element of the array, ALL strings have to equal for the function to return true so that would work fine.

When the array has length 0 the function will return true whcich I think is ok behaviour.

Finally, when a String reference is null, an error will occur obviously, as it would in chenG's original code, just add an if to handle that.
 

Posts:458
Registered: 7/1/03
Re: compare three strings  
Aug 10, 2004 6:55 AM (reply 9 of 30)



 
You could add the elements to a Set and compare the
size of the Set to the number of elements (Sets don't
allow duplicates). Example:
String[] s = {"one","two", "two"};List list = Arrays.asList(s);Set set = new HashSet(list);System.out.println(list.size() == set.size()); //false

Nice trick
 

Posts:24,036
Registered: 2/3/03
Re: compare three strings  
Aug 10, 2004 6:56 AM (reply 10 of 30)



 
You could add the elements to a Set and compare the
size of the Set to the number of elements (Sets
don't
allow duplicates). Example:
String[] s =</div>{"one",<div class="jive-quote">"two", "two"};List list = Arrays.asList(s);Set set = new HashSet(list);System.out.println(list.size() == set.size()); //false

Nice trick

Thanks. :o)
 

Posts:24,036
Registered: 2/3/03
Re: compare three strings  
Aug 10, 2004 7:01 AM (reply 11 of 30)



 
Second, yes I'm comparing the first element of the
array, ALL strings have to equal for the function to
return true so that would work fine.

My bad - I wasn't thinking clearly about this.
 

Posts:11,186
Registered: 06.04.04
Re: compare three strings  
Aug 10, 2004 7:28 AM (reply 12 of 30)



 
Second, yes I'm comparing the first element of the array, ALL strings have to equal for the function to
return true so that would work fine.

My bad - I wasn't thinking clearly about this.

Oatmeal for breakfast makes your mind go to la-la-land :-P

Jos
 

Posts:24,036
Registered: 2/3/03
Re: compare three strings  
Aug 10, 2004 7:30 AM (reply 13 of 30)



 
Second, yes I'm comparing the first element of the
array, ALL strings have to equal for the function to
return true so that would work fine.

My bad - I wasn't thinking clearly about this.

Oatmeal for breakfast makes your mind go to la-la-land
:-P

Jos

Lack of both sleep and coffee only heightens the "oatmeal" problem. I'm about as sharp as a pound of wet liver this morning.

Not much different than any other day, really...
 

Posts:14,142
Registered: 99-04-02
Re: compare three strings  
Aug 10, 2004 7:37 AM (reply 14 of 30)



 
...Second, even if it did,
you're only comparing the first element of the array.

um... doesn't logic dictate that if [1] thru [n] equal [0], any given index equals any other one?
 
This topic has 30 replies on 3 pages.    1 | 2 | 3 | Next »