Java program is not evaluating String value correctly...

S

Shelly

I have code where I get a string value from a file which contains the
State Abbreviation and compare it for state specific programming.

When it wasn't falling through the correct code, I put statements in to
prompt and tell me what was coming back in that field before it hit the
condition. The weird thing is, is that it is correct (no extra
spaces--nothing), but it doesn't like it. For further troubleshooting
purposes I set the field to what I wanted right before the condition
and then it works. The way I'm filling the state abbreviation string
(via a cell in an Excel Spreadsheet) is being used throughout the
program for other variable strings as well and is working just fine.

Here is the code...
***********************************************************************
** The ini.stateAbbr when checked here is WI no spaces or extra chars *
** But it goes to the "else" of this first condition. *
** Even if you can tell me other things to check would be great *
** because I'm looking at brick wall here. *
***********************************************************************

if (ini.stateAbbr == "WI") {
if (groupToPrint.masterNumber == "0") {
usablePath = groupToPrint.eMailAddress.trim()
+ " "
+ groupToPrint.stringGrNo
+ groupToPrint.masterNumber
+ groupToPrint.locNumber
+ groupToPrint.subNumber
+ groupToPrint.poolNumber
+ " "
+ groupToPrint.descriptiveName
+ theDate
+ theTime;
} else {
usablePath = groupToPrint.eMailAddress.trim()
+ " "
+ groupToPrint.masterNumber
+ groupToPrint.stringGrNo
+ groupToPrint.locNumber
+ groupToPrint.subNumber
+ groupToPrint.poolNumber
+ " "
+ groupToPrint.descriptiveName
+ theDate
+ theTime;
}
} else {
if (groupToPrint.masterNumber == "0") {
usablePath = groupToPrint.stringGrNo
+ groupToPrint.masterNumber
+ groupToPrint.locNumber
+ groupToPrint.subNumber
+ groupToPrint.poolNumber
+ groupToPrint.timePeriod
+ groupToPrint.descriptiveName
+ theDate
+ theTime;
} else {
usablePath = groupToPrint.masterNumber
+ groupToPrint.stringGrNo
+ groupToPrint.locNumber
+ groupToPrint.subNumber
+ groupToPrint.poolNumber
+ groupToPrint.timePeriod
+ groupToPrint.descriptiveName
+ theDate
+ theTime;
}
}
 
S

Sudsy

Shelly wrote:
***********************************************************************
** The ini.stateAbbr when checked here is WI no spaces or extra chars *
** But it goes to the "else" of this first condition. *
** Even if you can tell me other things to check would be great *
** because I'm looking at brick wall here. *
***********************************************************************

if (ini.stateAbbr == "WI") {
if (groupToPrint.masterNumber == "0") {
<snip>

Your comparison is wrong: you should be using String#equals, i.e.
if( ini.stateAbbr.equals( "WI" ) ) {
 
S

Steve W. Jackson

Shelly said:
:I have code where I get a string value from a file which contains the
:State Abbreviation and compare it for state specific programming.
:
:When it wasn't falling through the correct code, I put statements in to
:prompt and tell me what was coming back in that field before it hit the
:condition. The weird thing is, is that it is correct (no extra
:spaces--nothing), but it doesn't like it. For further troubleshooting
:purposes I set the field to what I wanted right before the condition
:and then it works. The way I'm filling the state abbreviation string
:(via a cell in an Excel Spreadsheet) is being used throughout the
:program for other variable strings as well and is working just fine.
:
:Here is the code...
:***********************************************************************
:** The ini.stateAbbr when checked here is WI no spaces or extra chars *
:** But it goes to the "else" of this first condition. *
:** Even if you can tell me other things to check would be great *
:** because I'm looking at brick wall here. *
:***********************************************************************
:
:if (ini.stateAbbr == "WI") {

Above is the source of your error. To compare a String variable to a
String value, use .equals() instead, so it becomes:

if (ini.stateAbbr.equals("WI")) {
:if (groupToPrint.masterNumber == "0") {
:usablePath = groupToPrint.eMailAddress.trim()
:+ " "
:+ groupToPrint.stringGrNo
:+ groupToPrint.masterNumber
:+ groupToPrint.locNumber
:+ groupToPrint.subNumber
:+ groupToPrint.poolNumber
:+ " "
:+ groupToPrint.descriptiveName
:+ theDate
:+ theTime;
:} else {
:usablePath = groupToPrint.eMailAddress.trim()
:+ " "
:+ groupToPrint.masterNumber
:+ groupToPrint.stringGrNo
:+ groupToPrint.locNumber
:+ groupToPrint.subNumber
:+ groupToPrint.poolNumber
:+ " "
:+ groupToPrint.descriptiveName
:+ theDate
:+ theTime;
:}
:} else {
:if (groupToPrint.masterNumber == "0") {
:usablePath = groupToPrint.stringGrNo
:+ groupToPrint.masterNumber
:+ groupToPrint.locNumber
:+ groupToPrint.subNumber
:+ groupToPrint.poolNumber
:+ groupToPrint.timePeriod
:+ groupToPrint.descriptiveName
:+ theDate
:+ theTime;
:} else {
:usablePath = groupToPrint.masterNumber
:+ groupToPrint.stringGrNo
:+ groupToPrint.locNumber
:+ groupToPrint.subNumber
:+ groupToPrint.poolNumber
:+ groupToPrint.timePeriod
:+ groupToPrint.descriptiveName
:+ theDate
: + theTime;
: }
:}

And the same for all other uses of == to String literals...

= Steve =
 
K

klynn47

I believe your problem has to do with the use of ==. When used between
object references, == returns a boolean indicating whether or not the
two references point to the same object. == between String literals
does test equality, but because there is only one copy of a String
literal maintained in memory. For testing equality between Strings,
it's best to use .equals
 
J

jeffc

Shelly said:
if (ini.stateAbbr == "WI") {

I think the problem is not that Java is evaluating String values incorrectly,
the problem is that you're not comparing String values! Remember that every
(object) variable is actually a reference, not the object itself. So you are
asking if a reference to a String is equal to a literal string. Those are
apples and oranges. As a newbie to the language myself, I'm a little surprised
it even compiles, actually. I'm not quite sure why comparison of a reference
value with a literal string would be allowed.
 
E

Eric Sosman

jeffc said:
I think the problem is not that Java is evaluating String values incorrectly,
the problem is that you're not comparing String values! Remember that every
(object) variable is actually a reference, not the object itself. So you are
asking if a reference to a String is equal to a literal string. Those are
apples and oranges. As a newbie to the language myself, I'm a little surprised
it even compiles, actually. I'm not quite sure why comparison of a reference
value with a literal string would be allowed.

The literal "WI" is itself a reference: it is a
reference to a two-character String object that gets
created behind the scenes, as it were, containing the
characters 'W' and 'I'. So the two things being
compared are two references, and the question being
asked is "Do these two refer to the same String object?"

And the answer (in Shelley's situation) is "No, they
refer to two distinct String objects." The two Strings
happen to have identical values -- they both contain 'W'
followed by 'I' and nothing else -- but they are distinct
String objects. There is a dollar bill in my wallet and
a dollar bill in your wallet, and although they have the
same value they are not the same dollar bill (if you do
not believe this, send me my dollar!).

The question Shelley really wants to ask is whether
the two Strings have identical values, not whether they
are the same String -- and as others have pointed out,
the way to do this is with the equals() method. This is
like recognizing that your dollar and my dollar have equal
value despite being different dollars, but that my dollar
and your ten-spot have different values. (Send me that
one, too.)
 
S

Shelly

Just wanted to say thank you for the help. Worked great (I'm new at
java so that stumpped me). Also, absolutely loved the "funnies" - made
my day!

Thanks to all of you and Happy Holidays! :)
 
J

jeffc

Eric Sosman said:
The literal "WI" is itself a reference: it is a
reference to a two-character String object that gets
created behind the scenes, as it were, containing the
characters 'W' and 'I'. So the two things being
compared are two references, and the question being
asked is "Do these two refer to the same String object?"

Ah, so if this had been some other sort of literal, like a primitive type 'c' or
3, then no object would have been created. Since it is a String literal, an
object is created. Is there any other Java literal for which an actual object
is created?
 
M

Michael Borgwardt

jeffc said:
Ah, so if this had been some other sort of literal, like a primitive type 'c' or
3, then no object would have been created. Since it is a String literal, an
object is created.

Yes. Note, though, that the object is created only once, when the class is loaded,
not each time the code is executed.
Is there any other Java literal for which an actual object
is created?

the .class literals also refer to objects, but of course those objects would
be created anyway, no matter whether there are literals that reference them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top