简体   繁体   中英

If not executing with location

I have an if stateement that check to make sure the zip code matches the area that we service but while I am in the zip code I am testing the If does not run. Please tell me what I'm doing wrong

String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
            String city = addresses.get(0).getLocality();
            String state = addresses.get(0).getAdminArea();
            String country = addresses.get(0).getCountryName();
            String postalCode = addresses.get(0).getPostalCode();
            String knownName = addresses.get(0).getFeatureName();
            // final TextView mTextView = (TextView) findViewById(R.id.location);
            //mTextView.setText("Latitude "+latitude+" Longitude "+longitude);
            final TextView mAdressTextView = (TextView) findViewById(R.id.addressNum);
            mAdressTextView.setText(address);
            final TextView mStateTextView = (TextView) findViewById(R.id.stateNum);
            mStateTextView.setText(city+ ", "+ state+", " + postalCode);

            if (postalCode == "33331"){
                Intent logoutDone = new Intent(MainActivity.this, Register.class);
                startActivity(logoutDone);
                Log.d("ADebugTag", "Value: " + postalCode);
            }

I'm not sure what you mean by the if does not run, but this answer may help.

In Java, the == compares the reference of your objects. Your goal is to see if the postalCode string object holds "33331". So you want to do this:

if(postalCode.equals("33331"))
{
   // do stuff
}

If you wanted to see if two objects were the same instance you would use == instead. When you want to compare the value of two strings, you need to use the equals() method as I did above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM