vendredi 5 janvier 2018

Why when I reverse a string in Java does it not equal the string before I reversed it? [duplicate]

This question already has an answer here:

I've tried a couple of different ways but none of them seem to work.

String str = s.nextLine();
String strSmall = str.toLowerCase();

String strOut = "";
String strReverse = "";
String[] strArr = strSmall.split(strOut);

for (int i = strArr.length - 1; i >= 0; i--) {
  strReverse += strArr[i];
}

if (str.length() < 3 || str.length() > 15) {
  System.out.println("thing");
} else if (str.length() > 3 && str.length() < 15) {
  if (strSmall == strReverse) {
    System.out.println(strReverse + " Did work");
  } else {
    System.out.println(strReverse + " Didn't work");
  }
}

for some reason the if statement, where I ask if strSmall is equal to strReverse, is outputting that it is false even when it should be true.

Aucun commentaire:

Enregistrer un commentaire