-2

the java subSequence is clearly true but only returns the false value. why? trying to see if a sequence is equal to a subsequence of a bigger string

package testifthen;


public class TestIfThen {

    public static void main(String[] args) {
String result = "01900287491234567489";
String result1 = "90028749";


if (result.subSequence(2, 10) == result1) {
        System.out.println("excel");
    }else {
        System.out.println("not found");

}
  }}
Spdwiz18
  • 31
  • 5
  • 4
    What language is this? It looks like Java, but you should add an explicit tag. – Stephen Newell May 08 '18 at 04:00
  • I clearly need to figure out how to ask questions better so I can find the answer without creating a duplicate question. I'm still learning and will do better next time.Thank you for the help. – Spdwiz18 May 08 '18 at 13:39

3 Answers3

2

It's hard to say without more information (for example what language is this in).

Assuming this is Java, I would say your problem is using == with strings instead of the .equals function. == doesn't check the contents of the string, only if they are referencing the same object. .equals should be used instead as it actually checks whether the characters match in the two strings

James Otter
  • 133
  • 4
1

In Java, the .equals method should be preferred to the == operator when checking for semantic equality. .equals should be used when you are checking if two values "mean" the same thing, whereas == checks if they're the same exact object.

Jackson
  • 559
  • 7
  • 20
1

Try using

if (result.subSequence(2, 10).equals(result1)) {
    System.out.println("excel");
} else {
    System.out.println("not found");
}

The == symbol might be the one causing it to return false because of the different references.

This post should explain more about differences between == and equals(): What is the difference between == vs equals() in Java?

Nitin Bisht
  • 5,053
  • 4
  • 14
  • 26
Stasis
  • 116
  • 7
  • This worked the way i needed. Thanks for a speedy answer. I'm self teaching java, so thanks for the web link. – Spdwiz18 May 08 '18 at 13:41