0
/*help me to correct this error*/

import javax.swing.JOptionPane;

public class Assignment1
{

    public static void main(String args[])
    {
        String input = JOptionPane.showInputDialog("Enter a string");
        if (istPalindrome(input))
        {
            JOptionPane.showMessageDialog(null,input+"is a palindrome");
        }
        else{
            JOptionPane.showMessageDialog(null ,input +"is not a palindrome");
        }

        public static boolean istPalindrome(String a)/*error is here*/
        {
            char[] charArray = word.toCharArray();
            int i1 = 0;
            int i2 = word.length() - 1;

            while (i2 > i1) {
                if (charArray[i1] != charArray[i2]) {
                    return false;
                }

                ++i1;
                --i2;
            }
            return true;
        }
    }
}
2
  • 3
    correct you indentation first, then the error should be obvious (the istPalindrome is defined inside the main function) Commented Feb 27, 2014 at 12:15
  • I`m learning java can you write that statement plz Commented Feb 27, 2014 at 12:24

2 Answers 2

2

As stated by ratchet freak, you need to move the function out of main. The function itself belongs to the Assignment1 class and as such needs to be:

import javax.swing.JOptionPane;

public class Assignment1
{
    public static void main(String args[])
    {
        String input = JOptionPane.showInputDialog("Enter a string");
        if (istPalindrome(input))
        {
            JOptionPane.showMessageDialog(null,input+"is a palindrome");
        }
        else{
            JOptionPane.showMessageDialog(null ,input +"is not a palindrome");
        }
    }

    public static boolean istPalindrome(String a)/*error is here*/
    {
        char[] charArray = a.toCharArray();
        int i1 = 0;
        int i2 = a.length() - 1;

        while (i2 > i1) {
            if (charArray[i1] != charArray[i2]) {
                return false;
            }

            ++i1;
            --i2;
        }
        return true;
    }
}

I've also changed the variable "word" over to "a" (for the statements word.toCharArray() and word.length()) due to the fact that there is no "word" variable in scope within that function.

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot create another function like that inside main function. Move the function out of the main function scope and call wherever necessary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.