• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing 2 objects (with no luck)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic