0

I have written a program, part of which checks whether two strings are equal or not. Although, both the strings when printed individually, come out to be same, however, when equated, the result so obtained is not what is desired. The code is as follows ..

    import java.util.*;
    import java.lang.*;
    import java.io.*;

    class GFG 
    {
        public static void main (String[] args) 
            {   
                StringBuilder pattern=new StringBuilder("^coal");
                StringBuilder text=new StringBuilder("coaltar");
                System.out.println(pattern.substring(1));
                System.out.println(text.substring(0,pattern.length()-1));
                if(pattern.charAt(0)=='^')
                {
                        if(pattern.substring(1)==text.substring(0,pattern.length()-1))
                                System.out.println("1a");
                        else
                                System.out.println("0a");
                }
            }
    }

The output must be 1a. However, it is coming out to be 0a. Please help.Thank You.

iamrkcheers
  • 373
  • 2
  • 7
  • 16
  • i would try `System.out.println( pattern.substring(1) ); System.out.println( text.substring(0,pattern.length()-1) );` after your first `if` statment. This will show you the results and see what the if statement is trying to compare. First note, your running a `substring()` on a StringBuilder. Is that correct? – Simon. Nov 28 '16 at 16:17
  • @Simon. You should read the duplicate, too. Whatever this code compares, it's *not* the content of the `StringBuilder`. And you should read the documentation for `StringBuilder` - it certainly has a `substring` method. – RealSkeptic Nov 28 '16 at 16:18

0 Answers0