0

Possible Duplicate:
How do I compare strings in Java?

I have the following code but for some reason it won't change the string

The accountClass returns the string "L"

String accountClass = accClass.substring(4, 5);
        if (accountClass == "A") {
            accountClass = "Staff - A";
        } else if (accountClass == "B") {
            accountClass = "Student - B";
        } else if (accountClass == "H") {
            accountClass = "Personal - H";
        } else if (accountClass == "K") {
            accountClass = "60 Plus - K";
        } else if (accountClass == "L") {
            accountClass = "Business - L";
        } else if (accountClass == "P") {
            accountClass = "Charity - P";
        } else if (accountClass == "Q") {
            accountClass = "AIB Subsidiary - Q";
        } else if (accountClass == "X") {
            accountClass = "Irish State Spons & Sub's - X";
        } else if (accountClass == "Y") {
            accountClass = "Other Companies & Orgs - Y";
        } else if (accountClass == "Z") {
            accountClass = "Impersonal & Gen. Ledger - Z";
        }

I'm just wondering if its the == causing the problem or something else?

Community
  • 1
  • 1
topcat3
  • 2,561
  • 6
  • 33
  • 57

2 Answers2

2

Always check string equality with equals() method. In case of Objects == checks if two reference variables refer to the same instance. to check if two strings are meaningfully equal use String.equals() method.

if (accountClass == "A") {

should be

if (accountClass.equals("A")) {

and so does your other if statements.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
-1

just realised it should be if (accountClass.equals("L"))

topcat3
  • 2,561
  • 6
  • 33
  • 57