Re: Java program is not evaluating String value correctly...

From: Steve W. Jackson (stevewjackson_at_charter.net)
Date: 12/09/04


Date: Thu, 09 Dec 2004 15:09:31 -0600

In article <1102622369.893892.116990@f14g2000cwb.googlegroups.com>,
 "Shelly" <mross@deltadentalwi.com> wrote:

>: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 Spread***) 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 =

-- 
Steve W. Jackson
Montgomery, Alabama