Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.length not working properly in java

Hi I have a code that checks if a string is palindrome or not.the code is like this:

         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 have written a code in c# that is exactly the same as the code above and its working very well but its not working properly in java and its output is always infinite. I stepped over my code and i think the problem is in the first if-statement of the CheckSymmetric() and it always jumps it but i don't know why.anybody can help me plz?

like image 635
Hooman Avatar asked Nov 29 '22 00:11

Hooman


2 Answers

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

In .NET, the second parameter is the length of the substring you're trying to take.

In Java, the second parameter is the exclusive end index of the substring you're trying to take.

For example:

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

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

You're trying to take a substring which ends 1 character before the end of the string, so you want

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

Lessons to take from this:

  • Assume your code is wrong before you assume that such a fundamental part of the platform is wrong. The chances of you discovering a bug in something as simple and fundamental as String.length() are virtually zero.
  • Use debugging and diagnostics to work out where the problem is: you should have been able to see that the Java code was trimming too much off the end of the string.
  • Don't assume that two methods with the same signature on different platforms will work exactly the same way. Read the documentation!
like image 84
Jon Skeet Avatar answered Dec 11 '22 05:12

Jon Skeet


length method in String returns the length of the string. Here you want to get the substring after remove first char and last char. Thus

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

should be:

    String sub = s.substring(1,s.length()-1);
like image 29
Rahul Avatar answered Dec 11 '22 03:12

Rahul