StringBuilder class compareTo not working on codechef IDE

class CodeChef{
  static boolean pali(StringBuilder temp1){
    StringBuilder temp=new StringBuilder(temp1);
    StringBuilder sb=new StringBuilder(temp1);
    //System.out.println(sb.charAt(2));
    if((sb.reverse()).compareTo(temp)==0){
      return true;
    }
    else{
      return false;
    }
  }
  static void sol(StringBuilder sb){
    int l=0, counter=0;
    int ar[]= new int[sb.length()];
    if(pali(sb)){
      System.out.println("YES"+"\n"+l);
      return;
    }
    for(int i=0;i<sb.length()-1;i++){
      // System.out.println("\n------------\n--> old string "+sb);
      if(sb.charAt(i) == sb.charAt(sb.length()-1-i)){
        continue;
      }
      else{
        // System.out.println("ense");
        if(sb.charAt(i+1) == sb.charAt(sb.length()-1-i) && (ar[i] == 0) && (ar[i+1] == 0)){
          // System.out.println("else 1");
          ar[i]=ar[i+1]=1;
          char c1=sb.charAt(i);
          char c2=sb.charAt(i+1);
          sb.setCharAt(i+1, c1);
          sb.setCharAt(i, c2);
          l++;
          // sb.setCharAt(i+1, sb.charAt(i));
          // sb.setCharAt(i, sb.charAt(i+1));
        }
        else if(sb.charAt(i) == sb.charAt(sb.length()-2-i) && (ar[sb.length()-1-i] == 0) && (ar[sb.length()-2-i] == 0)){
          ar[sb.length()-2-i]=ar[sb.length()-1-i]=1;
          l++;
          // System.out.println("else 2");
          char c1=sb.charAt(sb.length()-1-i);
          char c2=sb.charAt(sb.length()-2-i);
          sb.setCharAt(sb.length()-1-i, c2);
          sb.setCharAt(sb.length()-2-i, c1);
        }
        else{
          System.out.println("NO");
          return;
        }
      }
      if(pali(sb)){
        counter=1;
        break;
      }

      }
      if(counter == 1){
        System.out.println("YES"+"\n"+l);
        return;
      // System.out.println("--> new string "+sb);
    }
  }
  // static void sol(StringBuilder sb){
  //   for(int i=0;i<sb.length()-1;i++){
  //     char c1, c2;
  //     System.out.println("\n------------\nold string  "+sb);
  //     c1=sb.charAt(i);
  //     c2=sb.charAt(i+1);
  //     sb.setCharAt(i+1, c1);
  //     sb.setCharAt(i, c2);
  //     i++;
  //     System.out.println("new string  "+sb);
  //     if(pali(sb)){
  //       System.out.println();
  //     }
  //     System.out.println("new string  "+sb);
  //   }
  // }
  public static void main(String[] args) {
    Scanner in= new Scanner(System.in);
    int test=in.nextInt();
    while(test-- >0){
      int n=in.nextInt();
      in.nextLine();
      StringBuilder sb= new StringBuilder(in.nextLine());
    //  System.out.println(sb);
      // System.out.println(pali(sb));
      sol(sb);
    }
  }
}

Here is the code which I have written. The code is working fine on my laptop but is giving an error in the IDE of codechef. The error is

Main.java:7: error: cannot find symbol
if((sb.reverse()).compareTo(temp)==0){
^
symbol: method compareTo(StringBuilder)
location: class StringBuilder
1 error

Please help

Post the whole code?

No it isn’t :slight_smile:

Edit:

Anyway, StringBuilder.compareTo was introduced in Java 11 (September 2018), and I don’t know what version we’re running;

System.out.println(System.getProperty("java.version"));

gives

1.8.0_112

which I think is Java 8 … ? If so, then Codechef’s version of Java is not recent enough to have this method.

1 Like

Please see the entire code. I have updated the question

I think we can use StringBuilder class.

As it is used in this question

I didn’t suggest that you couldn’t use the class; I suggested you couldn’t use the StringBuilder.compareTo method.

1 Like

ohh… So is there any other way to compare the strings?
sb.equals(sb1) will not check the content, so I cannot use it.

Do I have to convert the stringbuilder to string to compare?

I don’t use Java, but it looks like, prior to Java 11, the best thing is to do just that:

1 Like

So converting to string is the only way.

Thanks for help :slight_smile:

1 Like