AsternMadkatz
AsternMadkatz

Reputation: 1

Java - Compare two strings and assign value to third variable?

this is my first so I'll try to add as much info as possible so I don't get yelled at. :-)

What I am trying to do is I have 2 variables that grab text from 2 fields and take only the first character from each and assign it to those values.

This is the code that I use to get the strings. They are 2 separate calls as you would.

try { var_ContactSurname = var_ContactSurname.substring(0,1);
    }
catch (Exception e){
}

I have the above again with a different variable. Now to this point it does what I want. It grabs the first letter from the fields and assigns it to the variables.

So at this point I have two variables (say with an example charater of D and R).

var_ContactSurname = R var_ContactLicenceNumber = D

What I want to do is compare those two variables and if they match I want to return a value of TRUE, else FALSE if they don't match.

That value has to be a string as well and be assigned to a new variable called var_ContactValidate.

if (var_ContactLicenceNumber.toLowerCase().equals()var_ContactSurname.toLowerCase()){
var_ContactValidate == "TRUE";
}
else {
var_ContactValidate == "FALSE";
}

No you may notice that there might be some code missing. I am using a rules engine that does a lot of the functions for me. I can use raw Java code to do other things (like this compare)...but that's the compare that I am having a problem with.

Any ideas for that compare would be greatly appreciated.

Thanks.

Upvotes: 0

Views: 2446

Answers (5)

Adriaan Koster
Adriaan Koster

Reputation: 16209

Here is an implementation that also checks for null values and empty Strings:

public class SurnameAndLicenseValidator {
    public static void main(String[] args) {

        // FALSE
        validateSurnameAndLicense(null, "jb78hq");
        validateSurnameAndLicense("Johnson", null);
        validateSurnameAndLicense(null, null);
        validateSurnameAndLicense("", "jb78hq");
        validateSurnameAndLicense("Johnson", "");
        validateSurnameAndLicense("", "");
        validateSurnameAndLicense("johnson", "xb78hq");

        // TRUE
        validateSurnameAndLicense("Johnson", "jb78hq");
        validateSurnameAndLicense("johnson", "jb78hq");
    }

    private static String validateSurnameAndLicense(String surname,
            String license) {
        String result;
        if (surname != null
                && surname.length() > 0
                && license != null
                && license.length() > 0
                && Character.toUpperCase(surname.charAt(0)) == Character
                        .toUpperCase(license.charAt(0))) {
            result = "TRUE";
        } else {
            result = "FALSE";
        }
        System.out.println(surname + " " + license + " " + result);
        return result;
    }
}

The main method is used as a unit test here. You might want to extract a real JUnit test from it, if you are into that kind of thing.

Upvotes: 0

Superfly
Superfly

Reputation: 601

Change your whole piece of code to:

if (var_ContactLicenceNumber.equalsIgnoreCase(var_ContactSurname)){
var_ContactValidate == "TRUE";
}
else {
var_ContactValidate == "FALSE";
}

This combines the case insensitivity that you want, and passes through the second string as an argument of the .equalsIgnoreCase function.

Also, I am not sure what you are trying to do with the line: var_ContactValidate == "TRUE";

If you want to assign var_ContactValidate to "TRUE" then use a single equals sign '=' as a double equals '==' compares the values instead. You may also considering using a boolean rather than a string in this case.

Upvotes: 1

amit
amit

Reputation: 178431

In addition to what already said - a simpler & more elegant version (without the if condition) could be:

var_ContactValidate = Boolean.toString(
     var_ContactLicenceNumber.equalsIgnoreCase(var_ContactSurname))
    .toUpperCase();

Upvotes: 1

aravindKrishna
aravindKrishna

Reputation: 440

if (var_ContactLicenceNumber.equalsIgnoreCase(var_ContactSurname){
var_ContactValidate = "TRUE";
}
else {
var_ContactValidate = "FALSE";
}

check it

Upvotes: 1

akf
akf

Reputation: 39485

  1. i would use the String method equalsIgnoreCase()
  2. to assign a value to a field, use a single =, not double (==).

Upvotes: 5

Related Questions