Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java says this method has a constructor name

I want to return the value of my array plus the return value of the recursive call.

However, for some reason, Java does not want to have the method name after the constructor.

In addition, when I tried to convert the method into another method, I get an error when I use isPalindrome.

I change my program around but I'm still getting errors.

public class isPalindrome {
    /**
     * This is the main entry point for the application
     * @return 
     */
    public static boolean main(String[] args) {
        
        String[] word = {"KayaK", "Desserts, I stressed"};

        
        
        boolean isPalindrome(String[] array, String s, String I) {
            
            if(i.charAt(0) == s.charAt(0)) {
                System.out.println("You entered a Palindrome");
                return true;
            }
            else {
                System.out.println("You didn't entered a Palindrome");
            }
        }
        
        
            try {
                System.in.read();
            } catch (Throwable t) {
            
            }
    }
}
like image 319
lonesarah Avatar asked Dec 02 '22 03:12

lonesarah


1 Answers

You can't use the class name as the name for a method. The only "methods" that can share a name with the class are constructors.

One fix would be to rename your class from isPalindrome to PalindromeFinder or something. That would also better align with Java naming conventions.

EDIT: Note that you never actually called your method in main; you tryed to assign a local variable to isPalindrome. That does not actually call the method. You would need to invoke the method with isPalindrome(...put your parameters here...) and store the result in a variable with a name that isn't being used.

Also note that a method can only return a single value (a single primitive or a single object). If you really want to return an array AND a boolean (and I'm not sure you do), you would have to store those in an object and return that object.

like image 156
Michael McGowan Avatar answered Dec 03 '22 17:12

Michael McGowan