Java says this method has a constructor name
Asked Answered
K

5

6

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) {
            
            }
    }
}
Kannada answered 2/3, 2011 at 3:31 Comment(2)
What is the array supposed to represent? In main you use s for a string array, but isPalindrome does not appear to use the String array for anything.Galibi
You cannot declare a method within another methodMinier
G
23

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.

Galibi answered 2/3, 2011 at 3:32 Comment(6)
+1 - following the Java naming conventions will avoid this "problem" in its entirety/Permalloy
I understand taht but how will i return my arrayKannada
return isPalindrome(array, s.substring(1, s.length()-1)); is the problemKannada
Would this work?? boolean isPalindrome = new boolean("true"); Boolean b = isPalindrome.booleanValue();Kannada
Lower case "boolean" is a primitive type, not an object. Therefore you can't try to call a constructor by saying "new boolean(...)" and you can't try to invoke methods on them like "isPalindrome.booleanValue()".Galibi
@user516805 - that's not even remotely valid Java syntax. And I can't see how it would help ... if it was.Permalloy
Y
0

In java you cannot use the class' name as a variable name. The error tells you that only the constructor method of the class may have the same name of the class, and a constructor cannot return int (it does not return anything).

Yoakum answered 2/3, 2011 at 3:34 Comment(1)
I understand taht but how will i return my arrayKannada
L
0

I would suggest rename the class to Palindrome or PalindromeHelper

Lyman answered 2/3, 2011 at 3:34 Comment(2)
I understand that but how will i return my arrayKannada
make the return type boolean[] instead of boolean I guess but I'm not sure about the parameters of that method.Lyman
P
0

but how will i return my array

return isPalindrome(array, s.substring(1, s.length()-1)); is the problem.

  1. As written, nothing actually creates an array to be returned. Hint: where is the return new String[]... or returnarray`?

  2. Nothing modifies array, so there's no point returning it.

  3. You can ... if it makes sense ... modify the array passed as an argument.

  4. If you really do want to return an array from isPalindrome, you can't* also return true and false. A method can only have one declared return type, and your code seems to "need" two; i.e. String[] and boolean. Can't do that.

* OK you can ... by declaring the return type to be Object. But that makes life difficult for the caller(s), and would be really bad design.

Permalloy answered 2/3, 2011 at 3:53 Comment(0)
O
0
public class isPalindrome {

    public static void main(String[] args) {
        System.out.println("hello");
    }
}

use 1st time file name is isPalindrome.your error is remove.

Outclass answered 16/6, 2024 at 10:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.