Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return boolean type in String function?

I’ve just started java, and I am stuck at basic problem of to check palindrome of string. I have come up with algorithm in which I reverse string then check if(str2.equals(str)) if its palindrome return true else false. But I am unable to return boolean in my string function due error

Syntax error on token "String", record expected

I tried typecasting but its not working. What is best way to return boolean or any other data type in another data type?

package fundamentals;
import java.util.*;
public class TestClass {

    
     public static boolean checkPalindrome(String str, String str2)
     {if(str2.equals(str)) {
            
            return true;
            }
         
        return false;
            
     }

//   
    public static String reverseString(String str) {    
        //Your code goes here
        String str2 = new String();
        int len = str.length();
        for(int i=len;i>0;i--)
        {  
            Character temp = str.charAt(i-1);
            str2= str2+temp;
            temp = null;
        }
        boolean ans =  checkPalindrome(str,str2);
        return ans;
                
                
                
        
    }


    public static void main (String[] args) {
        Scanner s =new Scanner(System.in);
       
        String str = new String();
        
        str = s.next();
        
        System.out.println(reverseString(str));
        
//      System.out.println(str);
//      
//      for(int i=0;i<str.length();i++)
//      {
//          char ch = str.charAt(i);
//          
//          System.out.println(ch);
//          
//      }
//      
        

     }

    
}

    

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

if you want to convert boolean to string, u can use the inbuilt Boolean.toString(boolean b) method

Here is the link to the doccumentation:
https://www.tutorialspoint.com/java/lang/boolean_tostring_boolean.htm#:~:text=toString(boolean%20b)%20returns%20a,%22false%22%20will%20be%20returned.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading