我有以下代码但由于某种原因它不会更改字符串
accountClass返回字符串“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";
}
我只是想知道它是= =导致问题还是别的什么?
答案 0 :(得分:2)
始终使用equals()方法检查字符串相等性。如果对象==
检查两个引用变量是否引用同一个实例。使用String.equals()
方法检查两个字符串是否有意义相等。
if (accountClass == "A") {
应该是
if (accountClass.equals("A")) {
你的其他if语句也是如此。
答案 1 :(得分:-1)
刚才意识到它应该是if(accountClass.equals(“L”))