...
> If(ri.getYards() == "yds"){
'If'? 'If' would not compile. Please post actual codes,
(copy/paste) rather than this pseudo-code (rubbish),
in future.
But to the question..
String comparison should be done on the contents
of the strings, rather than by comparing their
references (which is what happens above).
To compare the contents, change it to..
if( ri.getYards().equals("yds") ) {
..or better..
if ( ri.getYards()!=null &&
ri.getYards().equals("yds") ) {
Andrew T.
Lew - 03 Jan 2007 23:08 GMT
> To compare the contents, change it to..
>
[quoted text clipped - 4 lines]
> if ( ri.getYards()!=null &&
> ri.getYards().equals("yds") ) {
Also, two points about the data structure:
"string0" is a bad name on two counts: it is really just another way to say
"infoarray [0]" (should be "infoArray", or better, "info"), and calling
something "stringWhatever" or "whateverArray" embeds implementation in the
name, violating the principle of information hiding and giving absolutely no
clue as to the purpose of the variable. What is the self-documenting essential
connection between the fact that something is a String, at position 1, and
whether the contents represent the choice of yards or meters?
An array is supposed to represent a bunch of the same kinds of things,
usually, not many different kinds of things. (Seen from the problem domain,
not the implementation domain.)
The second point is that instead of an array where every element "means"
something different, you should consider creating an object whose fields (with
meaningful names) hold the information:
public class MetaInfo
{
String yardsOrMeters = "yds"; // better yet, use an enum!
...
}
- Lew
Andre Hinrichs - 04 Jan 2007 01:22 GMT
> ...
>> If(ri.getYards() == "yds"){
[quoted text clipped - 17 lines]
> if ( ri.getYards()!=null &&
> ri.getYards().equals("yds") ) {
You can omit the null check if you write this:
if ( "yds".equals(ri.getYards()) ) {
TechBookReport - 04 Jan 2007 10:27 GMT
>> ...
>>> If(ri.getYards() == "yds"){
[quoted text clipped - 20 lines]
>
> if ( "yds".equals(ri.getYards()) ) {
You're right, but personally I just don't like the way that code looks.
It's odd, but the aesthetics don't seem right to me.
Pan

Signature
TechBookReport Java - http://www.techbookreport.com/JavaIndex.html