0

The below code is part of a program. When the string s is passed as "one one" then in the first iteration, the value of the variable word is getting set to one but still, program is not getting inside the if statement. Please help.

for (String word : s.split(" ")) {

        if (word == "one") {

            if (d == 2) {
                r = s.concat("11");
                d = 0;
            }

            else if (t == 3) {
                r = s.concat("111");
                t = 0;
            }

            else {
                r = s.concat("1");
            }
        }

    }
Hgrbz
  • 185
  • 3
  • 12
Sachin
  • 1
  • 1
  • How about a real [mcve] instead of this? You know, something that compiles when copypasted into an online IDE. – Shark Apr 07 '22 at 11:33
  • 1
    you are comparing your Strings as if you're using JavaScript. Java isn't JavaScript – Stultuske Apr 07 '22 at 11:34
  • Don't use double-equals to compare strings in Java. The answer you will get is usually not what you want. Use `.equals()` instead, as in `word.equals("one")` – Robert Harvey Apr 07 '22 at 11:36

0 Answers0