0

I want to ask a question.

I feel so confused about my own coding because i think it is correct.

This is the issue.

public static void main(String[] args) {
String x = "Robert : Hi There";
String y = "Robert";
System.out.println(x.substring(0, x.indexOf(":")).trim());

if(x.substring(0, x.indexOf(":")).trim() != y){
       System.out.println("Pass");
   }
else
   {
       System.out.println("Not Pass");
   }
}

This gave me output:

Robert

Pass

I want the output is "Not Pass" but why did my coding gave another result.

I hope you can tell what is wrong.

Thank You.

davinma06
  • 23
  • 4

1 Answers1

1

You compare string objects. So you have to ue the equals method:

if(x.substring(0, x.indexOf(":")).trim().equls(y)){
Jens
  • 67,715
  • 15
  • 98
  • 113