Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is retuned, what am i checking

 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have to work on code that someone else has written, but not finished.

In the code ( a .JSP page) the following method is called

if (localiser.isUSA()))
do this;
else
do that;

The class localiser, does not have a method for isUSA(), so i have to write it. My question is what is being returned, or how does this check work as nothing is being passed in, so i dont know what to pass out.

I am at a muddle.

Thanks
Roger
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you want to use the output of that method in a conditional statement its going to have to have a boolean return value.
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks

i figured it was a boolean, but i dont know how to implement or how to use it. I have looked at the API and for it to work i obviously have to check something.

would the method go something like this?

public boolean isUSA(){
if(lang.equals("en"))
return true;
}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you implement this is up to you. I'd speak to whoever is setting the requirements. You'll need to find out what the check actually means, i.e. what set of circumstances need to be met for "isUSA" to be true.
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul

The check is as i wrote in the previous post.
It is classified as USA if the language is english, thats it.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If that's the case, I think you may have answered your own question (though your compiler will complain if you implement your method exactly as you suggest).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's fairly common to see:

Those of us who consider ourselves very clever and good looking like to shorten this a bit. Can you do it in one line?
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this, Maybe?



This should do the trick (i think)
 
Michael Valentino
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking more along the lines of:



or something of the like
[ May 30, 2006: Message edited by: Michael Valentino ]
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean that i am returning a String?

I tried to implement it the way you suggested but i got a complier error.

I am passing in the string in the mehod header, as in boolean isUSA(String lang)

My original question was what am i actually sending back to the calling method
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15435
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No! The statement:

You are calling the method equalsIgnoreCase of class String here. That method returns a boolean.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to chime in for all our friends over seas. Just because the language is english doesn't make it a USA location. Locales contain two parts, iso language code and iso country code. You need to test the country code portion to determine if the location is usa
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok

I am still getting a compiler error.

the JSP page is giving out about the localiser calling the method, isUSA();

its called like this :

The actual method in the Localiser class is :



Trying to simplify here, just to get the boolean working, will make it proper for international users then.
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CORRECTION TO LAST POST

the code should read


 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the USA -being a free country- allows you to install a Japanese JDK, or to set your computer to a timezone in South Africa, or to set the default language to German, all of which might result in the test not working the way you envision it
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roger Waters:
CORRECTION TO LAST POST

the code should read




isUsa expects a String argument to be passed to it. You're not passing any argument.
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok

Would someone be kind enough to show me how they would implement it, in both the class and the calling page.

I feel like i have started an issue on localisation, when all i wanted to do was get a boolean method to work.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What part of Joanne's post did you not understand?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
And the USA -being a free country- allows you to install a Japanese JDK, or to set your computer to a timezone in South Africa, or to set the default language to German, all of which might result in the test not working the way you envision it


�Qui�n dice que un Americano no puede hablar Espa�ol?
 
Roger Waters
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Thank you. your suggestion fixed my problem.

Bear, Thanks also. In haste i had not read joannes post. I thought it was mine.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be fair, Joanne's post was nearly simultaneous with yours. It's easy to miss in a case like that.

Incidentally, I have heard rumors (or were they rumours?) that some people may speak English outside the USA. Which seems an even more likely reason the result of this method may be incorrect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic