String.length not working properly in java

Hi I have a code that checks if a string is a palindrome or not. The code is as follows:

         package ProjeTarahi;

         import java.util.*;
         import java.io.File;
         import java.io.FileInputStream;
         import java.io.FileNotFoundException;
         import java.util.Scanner;
         import java.util.logging.Level;
         import java.util.logging.Logger;
         import java.lang.String;

         public class Main 
         {

         public boolean CheckIsSymmetric(String s)
            {
               if (s.length()<=1) 
               {
                   return true;

                }

        if (s.charAt(0)==s.charAt(s.length()-1))
        {
            String sub = s.substring(1,s.length()-2);
            return CheckIsSymmetric(sub);
        }
        else
        {
            return false;
        }


    }

    public static void main(String args[])throws FileNotFoundException
    {
        Scanner sc=new Scanner(new FileInputStream(new File("in.txt")));
        String input=sc.nextLine();
        Main p=new Main();

        if(p.CheckIsSymmetric(input)==true)
        {
            System.out.println("in reshte motegharen ast");
        }
        else 
        {
            System.out.println("infinite");
        }

    }
}

I wrote the code in C #, which is exactly the same as the code above, and its work very well, but it does not work properly in java, and its output is always infinite. I stepped over my code and I think the problem is with the first if statement of CheckSymmetric (), and it always skips, but I don't know why. Can anybody help me plz?

+4
source share
7 answers
public boolean checkIsSymmetric(String s) {
  return new StringBuilder(s).reverse().toString().equals(s);
}

Keep it simple and use the API.

0
source

This is the difference between String.Substring(int).NET and String.substring(int, int)Java.

.NET - , .

Java , .

:

// .NET
"Hello world".Substring(3, 4) => "lo w"

// Java
"Hello world".substring(3, 4) => "w"

, 1 ,

String sub = s.substring(1, s.length() - 1);

, :

  • , , , . , - , String.length(), .
  • , , : , Java .
  • , . !
+16

String . char last char. ,

    String sub = s.substring(1,s.length()-2);

:

    String sub = s.substring(1,s.length()-1);
+6

Java , :

beginIndex index endIndex - 1

, ,

 String sub = s.substring(1, s.length() - 1);
+2

Java- String.substring:
 * @param beginIndex , .
 * @param endIndex , .

, : s = "AbccbA"; System.out.println(s.substring(1, s.length() - 2));

: "bcc" "bccb", .

0

, :

String sub = s.substring(1,s.length()-2); to
String sub = s.substring(1,s.length()-1);

, IndexOutOfBoundsException, 2.

, , , , .

hardcode

//String input=sc.nextLine();
String input = "d";

, .

0

, , Computer-Science. String. , , . , . 13 . , -.

, ! -Cheers

p/s: : D

import java.util.Scanner;
public class Palindrome  {
   public static void main(String[]args){
      if(isReverse()){System.out.println("This is a palindrome.");}
      else{System.out.print("This is not a palindrome");}
   }
   public static boolean isReverse(){
     Scanner keyboard =  new Scanner(System.in);
      System.out.print("Please type something: "); 
      String line = ((keyboard.nextLine()).toLowerCase()).replaceAll("\\W","");
      return (line.equals(new StringBuffer(line).reverse().toString()));
   }
}

StringBuffer Java "/W" ( , , W). , !

0

Source: https://habr.com/ru/post/1523506/


All Articles