wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Comparing 2 objects (with no luck) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing 2 objects (with no luck)" Watch "Comparing 2 objects (with no luck)" New topic
Author

Comparing 2 objects (with no luck)

Roger Waters
Greenhorn

Joined: May 10, 2006
Posts: 14
Hi

I am trying to compare 2 objects to see if they are the same.
I am using java SDK 1.3.1

here is a snippet of whats going on (or going wrong)



i have to call it using the code for english and also great britain as i am on SDK 1.3.1

Any help is appreciated. i took me a while to get to this stage, what with using == and other such things to perform the comparison.

If someone can fix this (its probably really easy ) would they explain what i am doing wrong please.
Thanks
Roger
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

According to the API documentation, the equals method for Locale returns true if language, country, and variant are all identical. If you're getting unexpected results, it might be because of an unexpected variant.

You could try adding println statements to verify what you're working with -- calling getCountry(), getLanguage(), and getVariant() on both Locale instances.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Roger Waters
Greenhorn

Joined: May 10, 2006
Posts: 14
Marc

Thanks for the welcome.

I did use print statements to see if i was getting the right language and country and variant.

getCountry()and getLanguage() return the 2 digit strings en, gb
but the getVariant() returns an empty string.

Rgs
Roger
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If both instances of Locale are returning "en" for language, "GB" for country, and an empty String for variant, then I would expect the equals method to return true. Is this not what's happening?
Roger Waters
Greenhorn

Joined: May 10, 2006
Posts: 14
hi

I had another look at the code and :

Locale locale = req.getLocale(); is returning only 'en'.

But to create locale 'thisone' ,i have to call it this way
Locale thisone = new Locale("en", "gb", "");

So the following if statement,
if(locale.equals(thisone))
Will never be the same !

The problem now is how do i compare the two locales ?

I think i could do this !



This seems an awful convulouted way of doing things ?
Am I on the right track?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Roger Waters:
...Am I on the right track?

I guess I would back up and ask what the goal is in comparing these instances of Locale.

If the goal is to determine whether 2 Locales are both using the same language (considering that we can't compare countries), then yes, this is the approach I would use.
Roger Waters
Greenhorn

Joined: May 10, 2006
Posts: 14
Marc

This code worked a treat.

if (locale.getLanguage().equals(thisone.getLanguage())

It did what i wanted it to do, and that was display a greeting message to visitors from foreign soil in their native language.

Thanks for the help, guess i will be spending time on the ranch as i have a lot more to learn
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Comparing 2 objects (with no luck)